SDL Digest, Vol 18, Issue 9

Thanks for the tips. I changed the code and it effectively worked as expected. I have never really used a condition variable in the past. I just read about it here and there and tried to give it a spin. In my case, I merely use threading primitives to synchronize threads. In pthread, I use a barrier which is ideal for this. I could easily use a semaphore to implement a barrier, but was hoping that the condition variable would solve this somewhat more elegantly. It looks like nothing really implements it “cleanly”.

Chris> I hope you’re aware that you’re not making sure of anything by this? All

you’re doing is increasing the likelihood that the threads will be ready
(to probably close enough to 100% that it doesn’t matter in practice,
but still). Plus, you slow down your application launch needlessly. To
do this properly, you need to have the worker threads notify the main
thread when they’re ready, using another condition, or a semaphore.

Have you considered whether a semaphore might be a better solution for
what you’re trying to achieve than a condition? Unlike the latter, it
doesn’t “lose” the notifications that happen while no one is waiting on
it. And generally, I find that semaphores are easier to get your head
around than conditions. :slight_smile:

Hope this helps somewhat. Thread synchronization is complicated to do
properly, it took me a while to come to grips with it too, and I’m still
not sure I get it completely right (so corrections are welcome if
anything I wrote above is incorrect).