[thread.condvarany.intwait]

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

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

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

#### 32.7.5.3 Interruptible waits [thread.condvarany.intwait]

[1](#1)

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

The following wait functions will be notified
when there is a stop request on the passed stop_token[.](#1.sentence-1)

In that case the functions return immediately,
returning false if the predicate evaluates to false[.](#1.sentence-2)

[ð](#itemdecl:1)

`template<class Lock, class Predicate>
  bool wait(Lock& lock, stop_token stoken, Predicate pred);
`

[2](#2)

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

*Effects*: Registers for the duration of this call *this to get notified on a stop request on stoken during this call and then equivalent to:while (!stoken.stop_requested()) {if (pred())return true;
 wait(lock);}return pred();

[3](#3)

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

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

The returned value indicates whether the predicate evaluated totrue regardless of whether there was a stop request[.](#3.sentence-1)

â *end note*]

[4](#4)

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

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

[5](#5)

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

*Throws*: Any exception thrown by pred[.](#5.sentence-1)

[6](#6)

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

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

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

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

â *end note*]

[ð](#itemdecl:2)

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

[7](#7)

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

*Effects*: Registers for the duration of this call *this to get notified on a stop request on stoken during this call and then equivalent to:while (!stoken.stop_requested()) {if (pred())return true; if (wait_until(lock, abs_time) == cv_status::timeout)return pred();}return pred();

[8](#8)

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

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

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

â *end note*]

[9](#9)

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

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

The returned value indicates whether the predicate evaluated to true regardless of whether the timeout was triggered or a stop request was made[.](#9.sentence-1)

â *end note*]

[10](#10)

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

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

[11](#11)

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

*Throws*: Timeout-related exceptions ([[thread.req.timing]](thread.req.timing "32.2.4 Timing specifications")),
or any exception thrown by pred[.](#11.sentence-1)

[12](#12)

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

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

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

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

â *end note*]

[ð](#itemdecl:3)

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

[13](#13)

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

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