Search code examples
c++multithreadingboostsynchronizationboost-thread

Creating a 'synchronization point' between threads


I have a couple of boost::threads which all execute the same function.

void foo(){
    //Lock Mutex
    //Do some stuffs, part 1
    //Unlock Mutex
    //Do some stuffs, part 2
    //Lock Mutex
    //Do some stuffs, part 3
    //Unlock Mutex
}

In order for my application to work, it is necessary that, before executing part 2 in parallel, all the threads have finished executing part 1. I was not able to find any mechanism that would enable me to do that... am I missing something?

Thank you.


Solution

  • Use Boost barriers. Definition from official documentation:

    A barrier is a simple concept. Also known as a rendezvous, it is a synchronization point between multiple threads. The barrier is configured for a particular number of threads (n), and as threads reach the barrier they must wait until all n threads have arrived. Once the n-th thread has reached the barrier, all the waiting threads can proceed, and the barrier is reset.

    extracted from here.