33 Thread support library [thread]

33.5 Condition variables [thread.condition]

33.5.4 Class condition_­variable_­any [thread.condition.condvarany]

A Lock type shall meet the BasicLockable requirements.
[Note
:
All of the standard mutex types meet this requirement.
If a Lock type other than one of the standard mutex types or a unique_­lock wrapper for a standard mutex type is used with condition_­variable_­any, the user should ensure that any necessary synchronization is in place with respect to the predicate associated with the condition_­variable_­any instance.
end note
]
namespace std {
  class condition_variable_any {
  public:
    condition_variable_any();
    ~condition_variable_any();

    condition_variable_any(const condition_variable_any&) = delete;
    condition_variable_any& operator=(const condition_variable_any&) = delete;

    void notify_one() noexcept;
    void notify_all() noexcept;
    template <class Lock>
      void wait(Lock& lock);
    template <class Lock, class Predicate>
      void wait(Lock& lock, Predicate pred);

    template <class Lock, class Clock, class Duration>
      cv_status wait_until(Lock& lock, const chrono::time_point<Clock, Duration>& abs_time);
    template <class Lock, class Clock, class Duration, class Predicate>
      bool wait_until(Lock& lock, const chrono::time_point<Clock, Duration>& abs_time,
        Predicate pred);
    template <class Lock, class Rep, class Period>
      cv_status wait_for(Lock& lock, const chrono::duration<Rep, Period>& rel_time);
    template <class Lock, class Rep, class Period, class Predicate>
      bool wait_for(Lock& lock, const chrono::duration<Rep, Period>& rel_time,
        Predicate pred);
  };
}
condition_variable_any();
Effects: Constructs an object of type condition_­variable_­any.
Throws: bad_­alloc or system_­error when an exception is required ([thread.req.exception]).
Error conditions:
  • resource_­unavailable_­try_­again — if some non-memory resource limitation prevents initialization.
  • operation_­not_­permitted — if the thread does not have the privilege to perform the operation.
~condition_variable_any();
Requires: There shall be no thread blocked on *this.
[Note
:
That is, all threads shall have been notified; they may subsequently block on the lock specified in the wait.
This relaxes the usual rules, which would have required all wait calls to happen before destruction.
Only the notification to unblock the wait needs to happen before destruction.
The user should take care to ensure that no threads wait on *this once the destructor has been started, especially when the waiting threads are calling the wait functions in a loop or using the overloads of wait, wait_­for, or wait_­until that take a predicate.
end note
]
Effects: Destroys the object.
void notify_one() noexcept;
Effects: If any threads are blocked waiting for *this, unblocks one of those threads.
void notify_all() noexcept;
Effects: Unblocks all threads that are blocked waiting for *this.
template <class Lock> void wait(Lock& lock);
[Note
:
If any of the wait functions exits via an exception, it is unspecified whether the Lock is held.
One can use a Lock type that allows to query that, such as the unique_­lock wrapper.
end note
]
Effects:
  • Atomically calls lock.unlock() and blocks on *this.
  • When unblocked, calls lock.lock() (possibly blocking on the lock) and returns.
  • The function will unblock when signaled by a call to notify_­one(), a call to notify_­all(), or spuriously.
Remarks: If the function fails to meet the postcondition, terminate() shall be called ([except.terminate]).
[Note
:
This can happen if the re-locking of the mutex throws an exception.
end note
]
Postconditions: lock is locked by the calling thread.
Throws: Nothing.
template <class Lock, class Predicate> void wait(Lock& lock, Predicate pred);
Effects: Equivalent to:
while (!pred())
  wait(lock);
template <class Lock, class Clock, class Duration> cv_status wait_until(Lock& lock, const chrono::time_point<Clock, Duration>& abs_time);
Effects:
  • Atomically calls lock.unlock() and blocks on *this.
  • When unblocked, calls lock.lock() (possibly blocking on the lock) and returns.
  • The function will unblock when signaled by a call to notify_­one(), a call to notify_­all(), expiration of the absolute timeout ([thread.req.timing]) specified by abs_­time, or spuriously.
  • If the function exits via an exception, lock.lock() shall be called prior to exiting the function.
Remarks: If the function fails to meet the postcondition, terminate() shall be called ([except.terminate]).
[Note
:
This can happen if the re-locking of the mutex throws an exception.
end note
]
Postconditions: lock is locked by the calling thread.
Returns: cv_­status​::​timeout if the absolute timeout ([thread.req.timing]) specified by abs_­time expired, otherwise cv_­status​::​no_­timeout.
Throws: Timeout-related exceptions ([thread.req.timing]).
template <class Lock, class Rep, class Period> cv_status wait_for(Lock& lock, const chrono::duration<Rep, Period>& rel_time);
Effects: Equivalent to:
return wait_until(lock, chrono::steady_clock::now() + rel_time);
Returns: cv_­status​::​timeout if the relative timeout ([thread.req.timing]) specified by rel_­time expired, otherwise cv_­status​::​no_­timeout.
Remarks: If the function fails to meet the postcondition, terminate() shall be called ([except.terminate]).
[Note
:
This can happen if the re-locking of the mutex throws an exception.
end note
]
Postconditions: lock is locked by the calling thread.
Throws: Timeout-related exceptions ([thread.req.timing]).
template <class Lock, class Clock, class Duration, class Predicate> bool wait_until(Lock& lock, const chrono::time_point<Clock, Duration>& abs_time, Predicate pred);
Effects: Equivalent to:
while (!pred())
  if (wait_until(lock, abs_time) == cv_status::timeout)
    return pred();
return true;
[Note
:
There is no blocking if pred() is initially true, or if the timeout has already expired.
end note
]
[Note
:
The returned value indicates whether the predicate evaluates to true regardless of whether the timeout was triggered.
end note
]
template <class Lock, class Rep, class Period, class Predicate> bool wait_for(Lock& lock, const chrono::duration<Rep, Period>& rel_time, Predicate pred);
Effects: Equivalent to:
return wait_until(lock, chrono::steady_clock::now() + rel_time, std::move(pred));