Search⌘ K

Multithreading: Condition Variables

Explore how condition variables synchronize threads in C++ and understand common pitfalls like lost and spurious wakeups. Learn to use predicates properly to avoid race conditions, and discover when promises, futures, and std::async provide better alternatives for thread synchronization.

Condition Variables

Synchronizing threads via notifications is a simple concept, but condition variables make this task really challenging - mostly because the condition variables have no state.

  • If a condition variable gets a notification, it may be the wrong one spurious wakeup.
  • If a condition variable gets its notification before it was ready, the notification will be lost lost wakeup.

...