Situation
I’ve got a worker thread that’s being triggered by a condition_variable to do some work. Basically it is working very good and as supposed.
Problem
However there’s one problem: The thread launching the worker thread may get a long enough time slice to feed the data queue and notify the condition_variable when the worker thread is not ready yet, i.e. when it didn’t reach the condition_variable.wait() line yet.
Is there any way I can wait for a worker thread until the first call to wait() is made so it’s guaranteed that work will be processed when I notify the worker?
There’s no reason to do that. When the worker thread is ready, it will do the work. It won’t need to be triggered by a condition variable because it won’t ever wait.
Waiting for something that has already happened is a coding error. If you might ever even think of doing that, you fundamentally don’t understand condition variables.
Before a thread waits on a condition variable, it must make sure that there is something it needs to wait for. That thing must be protected by the mutex associated with the condition variable. And when the thread returns from the condition variable wait, it usually must re-test to see if it needs to wait again.