Errors Associated with C++ Condition Variable
The condition variable is prone to the following errors:
- Spurious Wakeup: Spurious wakeup refers to the condition when the consumer/receiver thread finishes its work before it is notified by the producer/sender. In the above example, we have used the variable data_ready precisely to cope with this error.
- Lost Wakeup: Lost wakeup refers to the condition when the sender sends the notification but there is no receiver in the wait for the notification yet.
Condition Variables in C++ Multithreading
In C++, the condition variable is a synchronization primitive that is used to notify the other threads in a multithreading environment that the shared resource is free to access. It is defined as the std::condition_variable class inside the <condition_variable> header file.
Prerequisite: C++ Multithreading, Mutex in C++.