Search code examples
c++multithreadingboostsynchronizationboost-thread

How to implement a dynamic thread Boost::Barrier?


Typically a thread barrier (i.e. boost::barrier) is initialized with an integer representing the number of threads that must call boost::barrier::wait - all threads wait at that point until the condition is met and then all threads continue.

Is it possible to implement a thread barrier that can have its 'waitCount' set after it has been initialized?

Or is there an equivalent approach that will give the same behaviour?

i.e. after we have done:

int numWaiting = 2;
boost::barrier b( numWaiting );

There are no methods to set a new numWaiting value;

The reason for wanting this is basically that the number of threads available for a process may increase AFTER the barrier was initialized but BEFORE the wait condition has been met.


Solution

  • You can add such behavior to simple barrier implementation based on boost::mutex. See code there: http://code.google.com/p/fengine/source/browse/trunk/src/engine/misc/barrier.hpp