Reputation: 111
What I'd like to know is how does std::condition_variable notify_all work as far as notifying all threads to wake.
The situation is that I have a main thread, a render thread, and each of those has its own thread pool to do whatever work it needs to. For example, if I have 6 logical cores, each thread pool would have 6 worker threads. These threads are woken using notify_all. Each thread pool has its own condition variable/mutex combo and are totally separate, so they do not conflict with each other.
The issue I'm having is that at times, when the render thread calls notify_all to wake up the worker threads, some will be woken immediately and some will take a long time, in the order of multiple milliseconds.
I have also noticed that this mostly happens when these threads are woken while the main thread workers are busy doing work of their own. So, at times of heavy load when there's already a thread per logical core working, trying to wake up these other threads exhibits this behavior. So out of the 6 threads that are waiting for the condition_variable, maybe 4 will be woken up, and the other two will not be until the load goes down.
My question is, is this expected behavior, or should I be looking to a bug in my code? Note, the threads always wake up, and under no load or light load, they usually wake up at around the same time. I also have no deadlocks or any other problems with my thread pool setup. So, am I just seeing the correct behavior of condition_variable where some threads can be woken quickly and others 4 or 5 ms later? How can I mitigate this behavior? Can anyone suggest an alternate way to wake up my worker threads that might work best?
Thanks in advance.
Upvotes: 1
Views: 528
Reputation: 54393
They cannot be woken up until they get scheduled. If other threads are running they get to complete their time-slice first.
How else would this work?
Upvotes: 2