[thread.condvarany.wait]

# 32 Concurrency support library [[thread]](./#thread)

## 32.7 Condition variables [[thread.condition]](thread.condition#thread.condvarany.wait)

### 32.7.5 Class condition_variable_any [[thread.condition.condvarany]](thread.condition.condvarany#thread.condvarany.wait)

#### 32.7.5.2 Noninterruptible waits [thread.condvarany.wait]

[ð](#lib:wait,condition_variable_any)

`template<class Lock>
  void wait(Lock& lock);
`

[1](#1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L10135)

*Effects*: 

- [(1.1)](#1.1)

  Atomically calls lock.unlock() and blocks on *this[.](#1.1.sentence-1)

- [(1.2)](#1.2)

  When unblocked, calls lock.lock() (possibly blocking on the lock) and returns[.](#1.2.sentence-1)

- [(1.3)](#1.3)

  The function will unblock when signaled by a call to notify_one(),
a call to notify_all(), or spuriously[.](#1.3.sentence-1)

[2](#2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L10145)

*Postconditions*: lock is locked by the calling thread[.](#2.sentence-1)

[3](#3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L10149)

*Throws*: Nothing[.](#3.sentence-1)

[4](#4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L10153)

*Remarks*: If the function fails to meet the postcondition, terminate() is invoked ([[except.terminate]](except.terminate "14.6.2 The std​::​terminate function"))[.](#4.sentence-1)

[*Note [1](#note-1)*:

This can happen if the re-locking of the mutex throws an exception[.](#4.sentence-2)

â *end note*]

[ð](#lib:wait,condition_variable_any_)

`template<class Lock, class Predicate>
  void wait(Lock& lock, Predicate pred);
`

[5](#5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L10169)

*Effects*: Equivalent to:while (!pred()) wait(lock);

[ð](#lib:wait_until,condition_variable_any)

`template<class Lock, class Clock, class Duration>
  cv_status wait_until(Lock& lock, const chrono::time_point<Clock, Duration>& abs_time);
`

[6](#6)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L10185)

*Effects*: 

- [(6.1)](#6.1)

  Atomically calls lock.unlock() and blocks on *this[.](#6.1.sentence-1)

- [(6.2)](#6.2)

  When unblocked, calls lock.lock() (possibly blocking on the lock) and returns[.](#6.2.sentence-1)

- [(6.3)](#6.3)

  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]](thread.req.timing "32.2.4 Timing specifications")) specified by abs_time,
or spuriously[.](#6.3.sentence-1)

- [(6.4)](#6.4)

  If the function exits via an exception, lock.lock() is called prior to exiting the function[.](#6.4.sentence-1)

[7](#7)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L10204)

*Postconditions*: lock is locked by the calling thread[.](#7.sentence-1)

[8](#8)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L10208)

*Returns*: cv_status​::​timeout if
the absolute timeout ([[thread.req.timing]](thread.req.timing "32.2.4 Timing specifications")) specified by abs_time expired,
otherwise cv_status​::​no_timeout[.](#8.sentence-1)

[9](#9)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L10214)

*Throws*: Timeout-related
exceptions ([[thread.req.timing]](thread.req.timing "32.2.4 Timing specifications"))[.](#9.sentence-1)

[10](#10)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L10219)

*Remarks*: If the function fails to meet the postcondition, terminate() is invoked ([[except.terminate]](except.terminate "14.6.2 The std​::​terminate function"))[.](#10.sentence-1)

[*Note [2](#note-2)*:

This can happen if the re-locking of the mutex throws an exception[.](#10.sentence-2)

â *end note*]

[ð](#lib:wait_for,condition_variable_any)

`template<class Lock, class Rep, class Period>
  cv_status wait_for(Lock& lock, const chrono::duration<Rep, Period>& rel_time);
`

[11](#11)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L10235)

*Effects*: Equivalent to:return wait_until(lock, chrono::steady_clock::now() + rel_time);

[12](#12)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L10242)

*Postconditions*: lock is locked by the calling thread[.](#12.sentence-1)

[13](#13)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L10246)

*Returns*: cv_status​::​timeout if
the relative timeout ([[thread.req.timing]](thread.req.timing "32.2.4 Timing specifications")) specified by rel_time expired,
otherwise cv_status​::​no_timeout[.](#13.sentence-1)

[14](#14)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L10252)

*Throws*: Timeout-related
exceptions ([[thread.req.timing]](thread.req.timing "32.2.4 Timing specifications"))[.](#14.sentence-1)

[15](#15)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L10257)

*Remarks*: If the function fails to meet the postcondition, terminate is invoked ([[except.terminate]](except.terminate "14.6.2 The std​::​terminate function"))[.](#15.sentence-1)

[*Note [3](#note-3)*:

This can happen if the re-locking of the mutex throws an exception[.](#15.sentence-2)

â *end note*]

[ð](#lib:wait_until,condition_variable_any_)

`template<class Lock, class Clock, class Duration, class Predicate>
  bool wait_until(Lock& lock, const chrono::time_point<Clock, Duration>& abs_time, Predicate pred);
`

[16](#16)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L10273)

*Effects*: Equivalent to:while (!pred())if (wait_until(lock, abs_time) == cv_status::timeout)return pred();return true;

[17](#17)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L10283)

[*Note [4](#note-4)*:

There is no blocking if pred() is initially true, or
if the timeout has already expired[.](#17.sentence-1)

â *end note*]

[18](#18)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L10289)

[*Note [5](#note-5)*:

The returned value indicates whether the predicate evaluates to true regardless of whether the timeout was triggered[.](#18.sentence-1)

â *end note*]

[ð](#lib:wait_for,condition_variable_any_)

`template<class Lock, class Rep, class Period, class Predicate>
  bool wait_for(Lock& lock, const chrono::duration<Rep, Period>& rel_time, Predicate pred);
`

[19](#19)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/threads.tex#L10303)

*Effects*: Equivalent to:return wait_until(lock, chrono::steady_clock::now() + rel_time, std::move(pred));
