##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::condition_variable_any

From cppreference.com
< cpp‎ | thread

Defined in header `<condition_variable>` |  |
class condition_variable_any; |  | (since C++11)

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 user-defined lock that meets the `BasicLockable` requirements.

`condition_variable_any` provides similar functionality to std::condition_variable. It is a synchronization primitive that can be used to block some number of threads until:

- a notification is received from another thread
- a timeout expires, or
- a spurious wakeup occurs

Any thread that intends to wait on `std::condition_variable_any` has to acquire a lock first. The wait operations atomically release the lock and suspend the execution of the thread. When the condition variable is notified, the thread is awakened, and the lock is reacquired.

The class `std::condition_variable_any` is a standard-layout class. It is not copy-constructible, move-constructible, copy-assignable, or move-assignable.

If the lock is std::unique_lock, std::condition_variable may provide better performance.

### [edit] Member functions

(constructor) | constructs the object (public member function) [edit]
(destructor) | destructs the object (public member function) [edit]
operator= [deleted] | not copy-assignable (public member function) [edit]
##### Notification
notify_one | notifies one waiting thread (public member function) [edit]
notify_all | notifies all waiting threads (public member function) [edit]
##### Waiting
wait | blocks the current thread until the condition variable is woken up (public member function) [edit]
wait_for | blocks the current thread until the condition variable is woken up or after the specified timeout duration (public member function) [edit]
wait_until | blocks the current thread until the condition variable is woken up or until specified time point has been reached (public member function) [edit]

### [edit] Notes

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]

### [edit] References

1. ↑ A. Williams (2012), "C++ concurrency in action" 9.2.4 Interupting a wait on std::condition_variable_any

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/thread/condition_variable_any&oldid=63436"