Search⌘ K
AI Features

New wait Overloads for the condition_variable_any

Explore the new wait overloads in std::condition_variable_any that accept std::stop_token and a predicate. Understand how these overloads enable cooperative thread interruption by allowing threads to respond to stop requests independently of notifications or timeouts, enhancing thread management and synchronization.

We'll cover the following...

The three wait variations to wait, wait_for, and wait_until of the std::condition_variable_any get new overloads. They take a std::stop_token.

C++
template <class Predicate>
bool wait(Lock& lock,
stop_token stoken,
Predicate pred);
template <class Rep, class Period, class Predicate>
bool wait_for(Lock& lock,
stop_token stoken,
const chrono::duration<Rep, Period>& rel_time,
Predicate pred);
template <class Clock, class Duration, class Predicate>
bool wait_until(Lock& lock,
stop_token stoken,
const chrono::time_point<Clock, Duration>& abs_time,
Predicate pred);

These new overloads need a predicate. The presented versions ensure that the threads be notified if a stop request for the passed ...