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 is std::unique_lock<std::mutex>, std::condition_variable may 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 a std::unique_lock (C++11) (class)

  1. ↑ 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