On 4/21/2013 5:45 AM, Vicente J. Botet Escriba wrote:
Le 21/04/13 10:27, Michael Marcin a écrit :
I recently ran across the need to spawn a thread and wait for it to finish its setup before continuing.
The accepted answer seems to be using a mutex and condition variable to achieve this.
However that work clutters up the code quite a bit with the implementation details.
I came across Java's CountDownLatch which does basically the same work but bundles it up into a tidy package.
What about using boost::barrier [1]?
Best, Vicente
Ah didn't know that existed. Looks like it serves a similar purpose. Still there are a few differences. Looks like barrier is a bit fatter to let it handle restarting on a new generation. Usage requires you to know the number of people that are going to call wait at construction time as well. IF you replaced the countdown_latch with barrier in my example you introduce more synchronization than is necessary. In addition to the constructor waiting on thread_func now thread_func must wait for the constructor.