Multithreaded Programming Guide

pthread_join Syntax

Use pthread_join(3C) to wait for a thread to terminate.

int pthread_join(pthread_t tid, void **status);
#include <pthread.h>

pthread_t tid;
int ret;
void *status;

/* waiting to join thread "tid" with status */
ret = pthread_join(tid, &status);

/* waiting to join thread "tid" without status */
ret = pthread_join(tid, NULL); 

The specified thread must be in the current process and must not be detached. For information on thread detachment, see Setting Detach State.

When status is not NULL, status points to a location that is set to the exit status of the terminated thread when pthread_join() returns successfully.

If multiple threads wait for the same thread to terminate, all the threads wait until the target thread terminates. Then one waiting thread returns successfully. The other waiting threads fail with an error of ESRCH.

After pthread_join() returns, any data storage associated with the terminated thread can be reclaimed by the application.