condition_variable - DryPerspective/Cpp17_jthread GitHub Wiki

Condition Variable

This library also offers an implementation of std::condition_variable_any which contains waiting functions that are able to be notified by a stop request. These are the same overloads for wait(), wait_for() and wait_until() which the standard class also received in C++20 to be able to wait on std::stop_token. These overloads are immune to missed-waits from registering the notification with the stop source and will always be notified by a stop request. If the stop request is made before the call to the waiting function, it will return immediately. The condition variable introduced in this library is no more immune to spurious wakeup than its standard equivalent, so it is recommend to use the waiting overloads which accept a predicate.

The library also makes std::condition_variable, std::cv_status, and std::notify_all_at_thread_exit visible in namespace dp for the sake of consistency when using this library's condition_variable_any alongside the standard contents of header <condition_variable>. These are just the standard types. The only new type introduced by this library is dp::condition_variable_any.

dp::condition_variable_any is found in condition_variable.h

Interface

notify_one Notifies one thread waiting on the condition variable.It is unspecified which thread is notified.
notify_all Notifies all threads waiting on the conditon variable.
wait Blocks the current thread until the condition variable is notified or spurious wakeup occurs.An overload which accepts a predicate is included in order to continue waiting on spurious wakeup.An overload which accepts a predicate and a dp::stop_token is included to notify the condition variable on a stop request.
wait_for Blocks the current thread until the condition variable is notified, spurious wakeup occurs, or a certain amount of time has passed.An overload which accepts a predicate is included in order to continue waiting on spurious wakeup.An overload which accepts a predicate and a dp::stop_token is included to notify the condition variable on a stop request.
wait_until Blocks the current thread until the condition variable is notified, spurious wakeup occurs, or until the provided time point.An overload which accepts a predicate is included in order to continue waiting on spurious wakeup.An overload which accepts a predicate and a dp::stop_token is included to notify the condition variable on a stop request.