Why architecture matters here

Condition variables fail on spurious wakeups + lost wakeups. Architecture matters because mutex + predicate + notify compose.

Advertisement

The architecture: every piece explained

The top strip is basics. Mutex. Predicate. wait(mutex). notify_one / notify_all.

The middle row is patterns. Spurious wakeup. Wait loop. Producer. Timed wait.

The lower rows are ops. Lost wakeup. Metrics. Ops — correct usage + testing + timeout.

Condition variable — predicate + wait + notify + spuriouswait for state change under mutexMutexprotects statePredicatecondition to wait onwait(mutex)release + blocknotify_one / notify_allwake waitersSpurious wakeupmust re-checkWait loopwhile (!pred)Producerstate change + notifyTimed waitwait_for / untilLost wakeupnotify before waitMetricswait time + queue depthOps — correct usage + testing + timeoutrecheckloopproducetimeoutavoidwatchwatchoperateoperate
Condition variable: wait under mutex until predicate holds.
Advertisement

End-to-end flow

End-to-end: consumer locks mutex, checks queue empty, wait(mutex). Producer locks mutex, pushes item, notify_one, unlocks. Consumer wakes, re-checks predicate, dequeues.