The Caveats of Condition Variables

In this lesson, we discuss the lost wakeup and spurious wakeup pitfall of condition variables with concurrency in C++

We'll cover the following

Lost Wakeup

The phenomenon of the lost wakeup is that the sender sends its notification before the receiver gets to a wait state. The consequence is that the notification is lost. The C++ standard describes condition variables as a simultaneous synchronization mechanism: “The condition_variable class is a synchronization primitive that can be used to block a thread, or multiple threads at the same time, …”. So, the notification gets lost and the receiver is waiting, and waiting, and…

Spurious Wakeup

It can happen that the receiver wakes up, although no notification happened. At a minimum, POSIX Threads and the Windows API can be victims of these phenomena.

In most of the use-cases, tasks are the less error-prone way to synchronize threads.

Get hands-on with 1200+ tech skills courses.