The MSDN page for SleepConditionVariableCS states that
Condition variables are subject to spurious wakeups (those not associated with an explicit wake) and stolen wakeups (another thread manages to run before the woken thread). Therefore, you should recheck a predicate (typically in a while loop) after a sleep operation returns.
As a result the conditional wait has to be enclosed in a while loop i.e.
while (check_predicate())
{
SleepConditionVariableCS(...)
}
If I were to use events instead of Conditional Variables can I do away with the while loop while waiting (WaitForSingleObject) for the event to be signaled?
For WaitForSingleObject()
, there are no spurious wakeups, so you can eliminate the loop.
If you use WaitForMultipleObjectsEx()
with bAlertable=TRUE
, MsgWaitForMultipleObjects()
with a wake mask, or MsgWaitForMultipleObjectsEx()
with bAlertable=TRUE
or a wake mask, then the wait can end on other conditions before the event is actually signaled.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With