std::condit...variable_any(3)
π NAME π
std::condition_variable_any β the icon, the moment, the class. This is the std::condit...variable_any(3) page from the C++ Standard Library manual, and bestie, itβs giving synchronization with range π
β¨
π¦ Synopsis π¦
Defined in header <condition_variable>
class condition_variable_any; (since C++11)
Okay so picture this: the condition_variable_any class is a generalization of std::condition_variable. Whereas std::condition_variable works only on std::unique_lock<std::mutex>, condition_variable_any can operate on any lock that meets the BasicLockable requirements. The way thatβs lowkey a glow-up for custom lock types? Obsessed.
See std::condition_variable for the description of the semantics of condition variables β same condition-variable energy, just a more flexible lock situation β¨
The class std::condition_variable_any is a StandardLayoutType. It is not CopyConstructible, MoveConstructible, CopyAssignable, or MoveAssignable. No cap, it is not out here being copied or moved around.
vibe check: if the lock isstd::unique_lock<std::mutex>,std::condition_variablemay provide better performance. So donβt be fake about it β pick the one that fits your lock π
π Member functions π
constructorβ constructs the object (public member function)destructorβ destructs the object (public member function)operator=β not copy-assignable,[deleted](public member function)
π Notification π
notify_oneβ notifies one waiting thread (public member function)notify_allβ notifies all waiting threads (public member function)
πΈ Waiting πΈ
waitβ blocks the current thread until the condition variable is awakened (public member function)wait_forβ blocks the current thread until the condition variable is awakened or after the specified timeout duration (public member function)wait_untilβ blocks the current thread until the condition variable is awakened or until specified time point has been reached (public member function)
π§ Notes π§
std::condition_variable_any can be used with std::shared_lock in order to wait on a std::shared_mutex in shared ownership mode. Shared ownership mode and condition-variable waiting? Itβs giving collaborative main character energy π«Ά
A possible use for std::condition_variable_any with custom Lockable types is to provide convenient interruptible waits: the custom lock operation would both lock the associated mutex as expected, and also perform the necessary setup to notify this condition variable when the interrupting signal is received.^[1] iykyk β thatβs the custom-lock interruptible-wait trick, and it ate.
π See also π
condition_variableβ provides a condition variable associated with astd::unique_lock(C++11) (class)
π External links π
- β Anthony Williams (2012, 1st ed./ 2019, 2nd ed.), βC++ Concurrency in Actionβ, 9.2.4 βInterrupting a wait on std::condition_variable_anyβ.
Source/version/date tea β: http://cppreference.com, 2024.06.10, page std::condit...variable_any(3) β mwah, go forth and synchronize, bestie πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms