Why must condition-variable waits use a predicate, and how do lost and spurious wakeups differ?
Show answer and explanation
A condition variable carries no application state, so the protected predicate is the durable fact a waiter needs. Waiting atomically unlocks the associated mutex and blocks, then reacquires it before returning; the predicate overload rechecks in a loop because a wait may wake spuriously. A lost wakeup is a protocol bug in which notification occurs before a waiter is actually waiting and no recorded predicate preserves the event; checking and changing the predicate under the same mutex prevents that gap. Publish the state first, notify according to the protocol, and never treat notification alone as proof that the condition holds.
A condition variable carries no application state, so the protected predicate is the durable fact a waiter needs. Waiting atomically unlocks the associated mutex and blocks, then reacquires it before returning; the predicate overload rechecks in a loop because a wait may wake spuriously. A lost wakeup is a protocol bug in which notification occurs before a waiter is actually waiting and no recorded predicate preserves the event; checking and changing the predicate under the same mutex prevents that gap. Publish the state first, notify according to the protocol, and never treat notification alone as proof that the condition holds.
Connect the conclusion directly to the type, lifetime, ownership, overload-resolution rule, or library contract present in the question. State implementation dependence or a violated precondition explicitly instead of merely guessing output.