#include <pthread.h>
int pthread_join(pthread_t target_thread, void **status);
#include <thread.h>
int thr_join(thread_t target_thread, thread_t *departed, void **status);
MT-Safe
Several threads cannot wait for the same thread to complete; one thread will complete successfully and the others will terminate with an error of ESRCH . pthread_join() or thr_join() will not block processing of the calling thread if the target target_thread has already terminated.
pthread_join() or thr_join() will return successfully when the target target_thread terminates.
If the pthread_join() calling thread is cancelled, then the target target_thread will remain joinable by pthread_join(). However, the calling thread may set up a cancellation cleanup handler on target_thread prior to the join call, which may detach the target thread by calling pthread_detach(3T) . (See pthread_detach(3T) and pthread_cancel(3T) .)
pthread_join() does not return the target_thread’s ID, as does the Solaris threads’ function thr_join(), and it does not cause the calling thread to wait for detached threads. pthread_join() returns ESRCH if the target is detached.
If the target target_thread ID is 0, thr_join() waits for any undetached thread in the process to terminate.
If departed is not NULL , it points to a location that is set to the ID of the terminated thread if thr_join() returns successfully.
while (thr_join(NULL , NULL , NULL ) == 0);
will wait for the termination of all other undetached and non-daemon threads; after which, EDEADLK will be returned.
pthread_join(3T) , on the other hand, must specify the target_thread ID for whose termination it will wait.
Calling pthread_join() also "detaches" the thread, that is, pthread_join() includes the effect of pthread_detach(). Hence, if a thread were to be cancelled when blocked in pthread_join(), an explicit detach would have to be done in the cancellation cleanup handler. In fact, the routine pthread_detach() exists mainly for this reason.