PTHREAD_JOIN(3)
π PTHREAD_JOIN(3) β the "so what happened to my thread" page
okay bestie, this one lives in section 3 of the Library Functions Manual (that's the libpthread, linked with -lpthread), and it's literally all about checking whether your thread is done yet. group-chat energy only, but the facts are sacred, so let's go π«Ά
β¨ NAME β¨
pthread_join, pthread_peekjoin_np, pthread_timedjoin_np, pthread_tryjoin_np β inspect thread termination state π
πΈ LIBRARY πΈ
POSIX Threads Library (libpthread, -lpthread)
π SYNOPSIS π
#include <pthread.h>
int
pthread_join(pthread_t thread, void **value_ptr);
#include <pthread_np.h>
int
pthread_peekjoin_np(pthread_t thread, void **value_ptr);
int
pthread_timedjoin_np(pthread_t thread, void **value_ptr,
const struct timespec *abstime);
int
pthread_tryjoin_np(pthread_t thread, void **value_ptr);
π DESCRIPTION π
pthread_join()
so pthread_join() literally suspends the calling thread until the target thread terminates β unless the target thread already terminated, in which case it's not gonna make you wait for nothing π
when a pthread_join() call returns successfully and you passed a non-NULL value_ptr, the value that the terminating thread passed to pthread_exit() gets stored in the location value_ptr points at. so that's where the exit goodies live π
when pthread_join() returns successfully, the target thread has been terminated. periodt.
β οΈ but bestie, heads up: the results of multiple simultaneous calls to pthread_join() that specify the same target thread are undefined. not "probably fine". undefined. don't do it π
and if the thread calling pthread_join() gets cancelled, then the target thread is not detached. important detail, don't forget it.
pthread_timedjoin_np()
this one is equivalent to pthread_join() except it'll return ETIMEDOUT if the target thread doesn't exit before the specified absolute time passes β° (yes, absolute. not a relative timeout. iykyk)
pthread_peekjoin_np()
the lowkey nosy one β it only peeks into the exit status of the specified thread π
- if the thread hasn't exited, you get the
EBUSYerror back - otherwise, zero is returned and the thread exit value is optionally stored into the location of
*value_ptr
and the cute part: the target thread is left unjoined, so you can totally use it as an argument for the pthread_join() family of functions again π
pthread_tryjoin_np()
joins the thread if it's already terminated, same as pthread_join(). if the thread hasn't terminated yet, the function returns EBUSY β¨ (no waiting, no drama)
the one that'll bite you π§
a thread that has exited but remains unjoined counts against [_POSIX_THREAD_THREADS_MAX]. yes, counts. so like, clean up after yourself bestie, those zombie-ish unjoined threads are not the vibe π
π RETURN VALUES π
if successful, the described functions return zero. otherwise an error number gets returned to indicate the error or special condition. simple as that π
π₯ ERRORS π₯
pthread_join(), pthread_peekjoin_np(), and pthread_timedjoin_np() will fail if:
[EINVAL]β the implementation detected that the value specified bythreaddoes not refer to a joinable thread π[ESRCH]β no thread could be found corresponding to the one specified by the given thread ID,threadπ»[EDEADLK]β a deadlock was detected, or the value ofthreadspecifies the calling thread π (yes, joining yourself. don't.)[EOPNOTSUPP]β the implementation detected that another caller is already waiting onthreadπ
additionally, pthread_timedjoin_np() will fail if:
[ETIMEDOUT]β the specified absolute time passed whilepthread_timedjoin_np()waited for thread exit β°
and pthread_peekjoin_np() and pthread_tryjoin_np() will also fail if:
[EBUSY]β the specified thread has not yet exited π«
π¦ SEE ALSO π¦
wait(2), pthread(3), pthread_create(3), pthread_np(3)
π STANDARDS π
pthread_join()conforms to ISO/IEC 9945-1:1996 (βPOSIX.1β) βpthread_timedjoin_np()is a FreeBSD extension which first appeared in FreeBSD 6.1 πΈpthread_peekjoin_np()is a FreeBSD extension which first appeared in FreeBSD 13.0 π¦pthread_tryjoin_np()is a FreeBSD extension which first appeared in FreeBSD 16.0 π₯
okay that's the whole page, no notes π this Debian build of the page is dated October 12, 2021, and honestly? iconic. go join your threads responsibly bestie, mwah πβ¨
π¦ where this page comes from
- freebsd 15.1: base 15.1
- Copyright (c) 1996-1998 John Birrell <jb@cimlogic.com.au>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms