Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blocking in pthread_join()

According to the manual page:

The pthread_join() function shall suspend execution of the calling thread until the target thread terminates, unless the target thread has already terminated.

So, as I understand, the calling process will block until the specified thread exit.

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); /* will be blocked here */
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 to pthread_join(thrs[0], NULL), until thrs[0] exit in some way. But how if another thread, for example, thrs[2] call pthread_exit() while we are blocked in the call to pthread_join(thrs[0], NULL)? Do we have to wait for thrs[0] to exit in order to receive the return value of thrs[2]?

like image 866
unix_newbie_programmer223 Avatar asked Jun 09 '15 13:06

unix_newbie_programmer223


People also ask

What is the use of pthread_join () function?

The pthread_join() function waits for a thread to terminate, detaches the thread, then returns the threads exit status. If the status parameter is NULL, the threads exit status is not returned.

What are the parameters of pthread_join?

PARAMETERS. Is the thread to wait for. Is the location where the exit status of the joined thread is stored. This can be set to NULL if the exit status is not required.

What happens if you don't use pthread_join?

If you want to be sure that your thread have actually finished, you want to call pthread_join . If you don't, then terminating your program will terminate all the unfinished thread abruptly. That said, your main can wait a sufficiently long time until it exits.

Does pthread_join terminate a thread?

The pthread_join() function blocks the calling thread until the specified thread terminates. The specified thread must be in the current process and must not be detached. For information on thread detachment, see Set Detach State.


1 Answers

But how if another thread, say thrs[2] exit while we are blocked in the call to pthread_join(thrs[0], NULL)?

Yes, it could happen. In that case, pthread_join(thrs[2], NULL); will return immediately.

Do we have to wait for thrs[0] to exit in order to receive the return value of thrs[2]?

Yes, you have to wait for thr[0] to terminate.


(Not directly related to the question)

It's not necessary to call pthread_join() on every thread you create. It's a convenient function to get the return status from thread(s). If you don't need to know the termination status of the thread, you could create the thread by seeting the "detached" attribute or call pthread_detach(pthread_self()); from the thread itself to make it detached. In some cases, you would want the threads created to continue execution but no longer need the main thread. In that case, you could call pthread_exit(NULL); from main thread which will let other threads to continue even after main thread exits.

like image 91
P.P Avatar answered Oct 31 '22 14:10

P.P