I have written a program using Boost threads. I have allocated two buffers (one for each of the worker threads) to store some info they are to process. They sit and wait for something to appear on the buffer, then they process it and place the results onto a different buffer. My buffer implementations are thread-safe. At present, I have the main thread busy-wait until the buffer size is zero, however this is really inefficient as it has to keep requesting control of the buffer and then checking the size. I'd like a way to sleep the main thread until BOTH worker threads signal that they are finished. I am fairly sure this can be achieved using boost::condition_variable
but I am unsure how I would do this with two worker threads.
Help greatly appreciated.
EDIT - Also, would boost::barrier
be suitable also?
You may also use thread::join method. This method will wait that the thread finish (the thread function return).
For more info about join.
I don't think that barrier can help you. Barrier allow to stop the thread processing until all the threads mets the barrier. When all the threads had called barrier::wait, then all the thread stop waiting.