Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is waiting for an event subject to spurious wakeups?

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?

like image 935
work.bin Avatar asked Feb 07 '23 10:02

work.bin


1 Answers

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.

like image 62
Remy Lebeau Avatar answered Feb 13 '23 11:02

Remy Lebeau