7

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!

JoHa
  • 1,989
  • 10
  • 28
  • 42

1 Answers1

4

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

caf
  • 233,326
  • 40
  • 323
  • 462
  • I do not understand, sorry for that. If you have a program like here: Multithreading in C++ article from geekforgeeks (cannot insert link) we can see that a std::join (NOT pthread_join()) if there for main not to terminate before the threads are running. However, the sequential order of joins does not give you the execution order. So, if I would use a barrier instead, I have the same PROCEDURE. Can you please clarify the difference maybe with an example? Thank you – user3742309 Aug 09 '20 at 12:38
  • @user3742309: This was answering the specific question about the difference between doing a `pthread_join` in a loop and using a barrier blocking the same set of threads instead. As the answer says, if you use a barrier then you can have the 'target' threads continue executing after they have synchronised with the main thread at the barrier. It sounds like your question might be substantially different to this one, so maybe ask it as a new question. – caf Aug 10 '20 at 05:54