Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pthread Barrier vs. Loop Join

So my question in C is: what is basically the differences (maybe pros and cons) of using a pthread barrier (init and wait..etc) compared to using the pthread Join in a loop.

So say I created 10 threads in a loop, and then later at the place of where I want a barrier, I put a loop to do Join for all the threads. Wouldn't that act as a Barrier too?

Please clarify. Thanks!

like image 912
JoHa Avatar asked Dec 06 '10 04:12

JoHa


People also ask

WHAT IS barrier in pthreads?

POSIX threads specifies a synchronization object called a barrier, along with barrier functions. The functions create the barrier, specifying the number of threads that are synchronizing on the barrier, and set up threads to perform tasks and wait at the barrier until all the threads reach the barrier.

Do you need Pthread join?

Yes if thread is attachable then pthread_join is must otherwise it creates a Zombie thread. Agree with answers above, just sharing a note from man page of pthread_join. After a successful call to pthread_join(), the caller is guaranteed that the target thread has terminated.

Can a Pthread join itself?

You can certainly call pthread_join() from the running thread itself, and as you have found out the call itself will handle it properly giving you an error code.


1 Answers

pthread_join() blocks the calling thread until the joining thread exits. In contrast, a barrier allows the all threads to continue running.

like image 78
caf Avatar answered Oct 13 '22 00:10

caf