std::condition_variable
is a synchronization primitive used with a std::mutex
to block one or more threads until another thread both modifies a shared variable (the condition) and notifies the std::condition_variable
.
In short, when you have at least two threads and you want to make one thread run when certain condition is met, you use condition variable.
To use condition variable, the thread that wants to modify the shared variable should:
- Acquire a
std::mutex
. - If the lock is owned, modify the shared variable.
- Call
std::condition_variable::notify_one()
.
The other thread that wants to wait for the condition variable to change needs to:
- Acquire a
std::mutex
(usually bestd::unique_lock<std::mutex>
) to protect the shared variable. - Call
std::condition_variable::wait()
(this function atomically releases the mutex and suspends thread execution until the condition variable is notified). - Check the condition and determine whether to run based on the conditon.
std::condition_variable::wait(std::unique_lock<std::mutex>& lock, Predicate pred)
lock
is the lock which must be locked by the calling thread.pred
is the predicate to check whether the waiting can be completed. It should be a FunctionObject.
wait
causes the current thread to block until the condition variable is notified or a spurious wakeup occurs.
wait
atomically calls lock.unlock()
and blocks this thread.
This thread will be unblocked when notify_one
or notify_all
is executed.
When this thread unblocked, call lock.lock()
.
-
std::condition_variable::notify_one()
If any threads are waiting on this condition variable, callingnotify_one
unblocks one of the waiting threads. -
std::condition_variable::notify_all()
If any threads are waiting on this condition variable, callingnotify_all
unblocks all of the waiting threads.
example in condition_variable