According to the manual page:
The pthread_join () function should pause the call until the target thread terminates, unless the target thread has already completed.
So, as I understand it, the calling process will block until the specified thread terminates.
Now consider the following code:
pthread_t thrs[NUMTHREADS];
for (int i = 0; i < NUMTHREADS; i++)
{
pthread_create(&thrs[i], NULL, thread_main, NULL);
}
pthread_join(thrs[0], NULL);
pthread_join(thrs[1], NULL);
pthread_join(thrs[2], NULL);
pthread_join(thrs[NUMTHREADS - 1], NULL);
The calling thread will be blocked in the call pthread_join(thrs[0], NULL)until thrs[0]it somehow exits. But how, if another thread, for example, thrs[2]calls pthread_exit(), and we are blocked during a call pthread_join(thrs[0], NULL)? Do I have to wait for the exit thrs[0]to get the return value thrs[2]?
source
share