condition_variable::wait() called with no predicate
ID |
c.correctness.condition_variable_wait_without_predicate |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Concurrency |
Language |
C / C++ |
Description
This call waits on the condition variable with no predicate, so it wakes on any notification — including a spurious wakeup that the standard explicitly allows — and proceeds without checking that the awaited condition actually holds. Pass a predicate: cv.wait(lock, [] { return condition; }), which loops internally until the predicate is true, correctly handling both spurious wakeups and lost wakeups from notifications sent before the wait began.
Rationale
This call waits on the condition variable with no predicate, so it wakes on any notification — including a spurious wakeup that the standard explicitly allows — and proceeds without checking that the awaited condition actually holds. Pass a predicate: cv.wait(lock, [] { return condition; }), which loops internally until the predicate is true, correctly handling both spurious wakeups and lost wakeups from notifications sent before the wait began.
The following code illustrates the pattern detected by this rule:
std::mutex m;
std::condition_variable cv;
void waiter()
{
std::unique_lock<std::mutex> lock(m);
// FLAGGED: condition_variable::wait() called with no predicate
cv.wait(lock);
}