@nassostrez I think the main difference between .join() and the barriers is that .join() waits for the threads to fully complete their task, whereas barriers allow for intra-task synchronization.
You could imagine we have some function where it would be really useful for all the threads to synchronize halfway through the function. Instead of splitting the function into two pieces f1() and f2(), creating a bunch of threads for f1() and then joining them all, and then creating a bunch of threads for f2(), we could just insert a barrier in the middle of the original function and launch threads to take care of the original function. I would guess that there's less overhead in this method of communication than having to join everything and respawn all the threads.
Maybe a more concrete example of where a barrier could be helpful is suppose I have a function with a std::atomic
Thanks @ardenma for the detailed explanation. It's super helpful!
Please log in to leave a comment.
I was surprised to learn about the concept of barrier. Until now, I thought of multi-threading as giving work to some threads to execute and then just wait for them to finish (with .join()).
I guess my question is, how should we think about using a barrier. And what is its overhead, I would guess that it is not as lightweight as a simple lock.