Lock in pthread_join ()

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); /* 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 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]?

+4
source share
3 answers

- , thrs[0], thrs[2], thrs[[0] thrs[1].

, - - , - , . / ( pthread_join(), , , , int he queue/signal).

+2

, , thrs [2], , pthread_join (thrs [0], NULL)?

, . pthread_join(thrs[2], NULL); .

, thrs [0] , thrs [2]?

, thr[0].


( )

pthread_join() . . , , "" pthread_detach(pthread_self()); , . , , . pthread_exit(NULL); , .

+3

. .

+1

All Articles