32 Thread support library [thread] (original) (raw)

32.1 General [thread.general]

The following subclauses describe components to create and managethreads, perform mutual exclusion, and communicate conditions and values between threads, as summarized in Table 146.

Table 146: Thread support library summary [tab:thread.summary]

Subclause Header
[thread.req] Requirements
[thread.stoptoken] Stop tokens <stop_­token>
[thread.threads] Threads <thread>
[thread.mutex] Mutual exclusion <mutex>, <shared_­mutex>
[thread.condition] Condition variables <condition_­variable>
[thread.sema] Semaphores <semaphore>
[thread.coord] Coordination types <latch> <barrier>
[futures] Futures <future>

32.2 Requirements [thread.req]

32.2.1 Template parameter names [thread.req.paramname]

Throughout this Clause, the names of template parameters are used to express type requirements.

If a template parameter is named Predicate, operator() applied to the template argument shall return a value that is convertible to bool.

If a template parameter is named Clock, the corresponding template argument shall be a type Cfor which is_­clock_­v<C> is true; otherwise the program is ill-formed.

32.2.2 Exceptions [thread.req.exception]

Some functions described in this Clause are specified to throw exceptions of typesystem_­error ([syserr.syserr]).

Such exceptions are thrown if any of the function's error conditions is detected or a call to an operating system or other underlying API results in an error that prevents the library function from meeting its specifications.

[ Example

:

Consider a function in this clause that is specified to throw exceptions of typesystem_­error and specifies error conditions that includeoperation_­not_­permitted for a thread that does not have the privilege to perform the operation.

Assume that, during the execution of this function, an errnoof EPERM is reported by a POSIX API call used by the implementation.

Since POSIX specifies an errno of EPERM when “the caller does not have the privilege to perform the operation”, the implementation maps EPERM to anerror_­condition of operation_­not_­permitted ([syserr]) and an exception of type system_­error is thrown.

end example

]

The error_­code reported by such an exception's code() member function compares equal to one of the conditions specified in the function's error condition element.

32.2.3 Native handles [thread.req.native]

Several classes described in this Clause have members native_­handle_­type andnative_­handle.

The presence of these members and their semantics isimplementation-defined.

[ Note

:

These members allow implementations to provide access to implementation details.

Their names are specified to facilitate portable compile-time detection.

Actual use of these members is inherently non-portable.

end note

]

32.2.4 Timing specifications [thread.req.timing]

Several functions described in this Clause take an argument to specify a timeout.

These timeouts are specified as either a duration or a time_­point type as specified in [time].

Implementations necessarily have some delay in returning from a timeout.

Any overhead in interrupt response, function return, and scheduling induces a “quality of implementation” delay, expressed as duration .

Ideally, this delay would be zero.

Further, any contention for processor and memory resources induces a “quality of management” delay, expressed as duration.

The delay durations may vary from timeout to timeout, but in all cases shorter is better.

The functions whose names end in _­for take an argument that specifies a duration.

These functions produce relative timeouts.

Implementations should use a steady clock to measure time for these functions.326

Given a duration argument , the real-time duration of the timeout is .

The functions whose names end in _­until take an argument that specifies a time point.

These functions produce absolute timeouts.

Implementations should use the clock specified in the time point to measure time for these functions.

Given a clock time point argument , the clock time point of the return from timeout should be when the clock is not adjusted during the timeout.

If the clock is adjusted to the time during the timeout, the behavior should be as follows:

An implementation returns from such a timeout at any point from the time specified above to the time it would return from a steady-clock relative timeout on the difference between and the time point of the call to the _­until function.

[ Note

:

Implementations should decrease the duration of the wait when the clock is adjusted forwards.

end note

]

[ Note

:

If the clock is not synchronized with a steady clock, e.g., a CPU time clock, these timeouts might not provide useful functionality.

end note

]

The resolution of timing provided by an implementation depends on both operating system and hardware.

The finest resolution provided by an implementation is called thenative resolution.

Implementation-provided clocks that are used for these functions meet theCpp17TrivialClock requirements ([time.clock.req]).

A function that takes an argument which specifies a timeout will throw if, during its execution, a clock, time point, or time duration throws an exception.

Such exceptions are referred to as timeout-related exceptions.

[ Note

:

Instantiations of clock, time point and duration types supplied by the implementation as specified in [time.clock] do not throw exceptions.

end note

]

32.2.5 Requirements for Cpp17Lockable types [thread.req.lockable]

32.2.5.1 In general [thread.req.lockable.general]

An execution agent is an entity such as a thread that may perform work in parallel with other execution agents.

[ Note

:

Implementations or users can introduce other kinds of agents such as processes or thread-pool tasks.

end note

]

The calling agent is determined by context, e.g., the calling thread that contains the call, and so on.

[ Note

:

Some lockable objects are “agent oblivious” in that they work for any execution agent model because they do not determine or store the agent's ID (e.g., an ordinary spin lock).

end note

]

The standard library templates unique_­lock ([thread.lock.unique]),shared_­lock ([thread.lock.shared]),scoped_­lock ([thread.lock.scoped]),lock_­guard ([thread.lock.guard]), lock,try_­lock ([thread.lock.algorithm]), andcondition_­variable_­any ([thread.condition.condvarany]) all operate on user-supplied lockable objects.

The Cpp17BasicLockable requirements, the Cpp17Lockable requirements, and the Cpp17TimedLockable requirements list the requirements imposed by these library types in order to acquire or release ownership of a lock by a given execution agent.

[ Note

:

The nature of any lock ownership and any synchronization it entails are not part of these requirements.

end note

]

32.2.5.2 Cpp17BasicLockable requirements [thread.req.lockable.basic]

A type L meets the Cpp17BasicLockable requirements if the following expressions are well-formed and have the specified semantics (m denotes a value of type L).

Effects:Blocks until a lock can be acquired for the current execution agent.

If an exception is thrown then a lock shall not have been acquired for the current execution agent.

Preconditions:The current execution agent holds a lock on m.

Effects:Releases a lock on m held by the current execution agent.

32.2.5.3 Cpp17Lockable requirements [thread.req.lockable.req]

A type L meets the Cpp17Lockable requirements if it meets the Cpp17BasicLockablerequirements and the following expressions are well-formed and have the specified semantics (m denotes a value of type L).

Effects:Attempts to acquire a lock for the current execution agent without blocking.

If an exception is thrown then a lock shall not have been acquired for the current execution agent.

Returns: true if the lock was acquired, false otherwise.

32.2.5.4 Cpp17TimedLockable requirements [thread.req.lockable.timed]

A type L meets the Cpp17TimedLockable requirements if it meets the Cpp17Lockablerequirements and the following expressions are well-formed and have the specified semantics (m denotes a value of type L, rel_­time denotes a value of an instantiation of duration, and abs_­time denotes a value of an instantiation of time_­point).

Effects:Attempts to acquire a lock for the current execution agent within the relative timeout ([thread.req.timing]) specified by rel_­time.

The function will not return within the timeout specified by rel_­time unless it has obtained a lock on mfor the current execution agent.

If an exception is thrown then a lock has not been acquired for the current execution agent.

Returns: true if the lock was acquired, false otherwise.

m.try_lock_until(abs_time)

Effects:Attempts to acquire a lock for the current execution agent before the absolute timeout ([thread.req.timing]) specified by abs_­time.

The function will not return before the timeout specified by abs_­time unless it has obtained a lock on m for the current execution agent.

If an exception is thrown then a lock has not been acquired for the current execution agent.

Returns: true if the lock was acquired, false otherwise.

32.3 Stop tokens [thread.stoptoken]

32.3.1 Introduction [thread.stoptoken.intro]

This clause describes components that can be used to asynchonously request that an operation stops execution in a timely manner, typically because the result is no longer required.

Such a request is called a stop request.

stop_­source, stop_­token, and stop_­callbackimplement semantics of shared ownership of a stop state.

Any stop_­source, stop_­token, or stop_­callbackthat shares ownership of the same stop state is an associated stop_­source, stop_­token, or stop_­callback, respectively.

The last remaining owner of the stop state automatically releases the resources associated with the stop state.

A stop_­token can be passed to an operation which can either

A stop request made via a stop_­source will be visible to all associated stop_­token and stop_­source objects.

Once a stop request has been made it cannot be withdrawn (a subsequent stop request has no effect).

Callbacks registered via a stop_­callback object are called when a stop request is first made by any associated stop_­source object.

Calls to the functions request_­stop, stop_­requested, and stop_­possibledo not introduce data races.

A call to request_­stop that returns truesynchronizes with a call to stop_­requestedon an associated stop_­token or stop_­source object that returns true.

Registration of a callback synchronizes with the invocation of that callback.

32.3.2 Header <stop_­token> synopsis [thread.stoptoken.syn]

namespace std {

class stop_token;

class stop_source;

struct nostopstate_t { explicit nostopstate_t() = default; }; inline constexpr nostopstate_t nostopstate{};

template class stop_callback; }

32.3.3 Class stop_­token [stoptoken]

The class stop_­token provides an interface for querying whether a stop request has been made (stop_­requested) or can ever be made (stop_­possible) using an associated stop_­source object ([stopsource]).

A stop_­token can also be passed to astop_­callback ([stopcallback]) constructor to register a callback to be called when a stop request has been made from an associated stop_­source.

namespace std { class stop_token { public:

stop_token() noexcept;

stop_token(const stop_token&) noexcept;
stop_token(stop_token&&) noexcept;
stop_token& operator=(const stop_token&) noexcept;
stop_token& operator=(stop_token&&) noexcept;
~stop_token();
void swap(stop_token&) noexcept;


[[nodiscard]] bool stop_requested() const noexcept;
[[nodiscard]] bool stop_possible() const noexcept;

[[nodiscard]] friend bool operator==(const stop_token& lhs, const stop_token& rhs) noexcept;
friend void swap(stop_token& lhs, stop_token& rhs) noexcept;

}; }

32.3.3.1 Constructors, copy, and assignment [stoptoken.cons]

Postconditions: stop_­possible() is false andstop_­requested() is false.

[ Note

:

Because the created stop_­token object can never receive a stop request, no resources are allocated for a stop state.

end note

]

stop_token(const stop_token& rhs) noexcept;

Postconditions: *this == rhs is true.

[ Note

:

*this and rhs share the ownership of the same stop state, if any.

end note

]

stop_token(stop_token&& rhs) noexcept;

Postconditions: *this contains the value of rhsprior to the start of construction and rhs.stop_­possible() is false.

Effects:Releases ownership of the stop state, if any.

stop_token& operator=(const stop_token& rhs) noexcept;

Effects:Equivalent to: stop_­token(rhs).swap(*this).

stop_token& operator=(stop_token&& rhs) noexcept;

Effects:Equivalent to: stop_­token(std​::​move(rhs)).swap(*this).

void swap(stop_token& rhs) noexcept;

Effects:Exchanges the values of *this and rhs.

32.3.3.2 Members [stoptoken.mem]

[[nodiscard]] bool stop_requested() const noexcept;

Returns: true if *this has ownership of a stop state that has received a stop request; otherwise, false.

[[nodiscard]] bool stop_possible() const noexcept;

Returns: false if:

otherwise, true.

32.3.3.3 Non-member functions [stoptoken.nonmembers]

[[nodiscard]] bool operator==(const stop_token& lhs, const stop_token& rhs) noexcept;

Returns: true if lhs and rhs have ownership of the same stop state or if both lhs and rhs do not have ownership of a stop state; otherwise false.

friend void swap(stop_token& x, stop_token& y) noexcept;

Effects:Equivalent to: x.swap(y).

32.3.4 Class stop_­source [stopsource]

The class stop_­source implements the semantics of making a stop request.

A stop request made on a stop_­source object is visible to all associated stop_­source and stop_­token ([stoptoken]) objects.

Once a stop request has been made it cannot be withdrawn (a subsequent stop request has no effect).

namespace std {

struct nostopstate_t { explicit nostopstate_t() = default; }; inline constexpr nostopstate_t nostopstate{};

class stop_source { public:

stop_source();
explicit stop_source(nostopstate_t) noexcept;

stop_source(const stop_source&) noexcept;
stop_source(stop_source&&) noexcept;
stop_source& operator=(const stop_source&) noexcept;
stop_source& operator=(stop_source&&) noexcept;
~stop_source();
void swap(stop_source&) noexcept;


[[nodiscard]] stop_token get_token() const noexcept;
[[nodiscard]] bool stop_possible() const noexcept;
[[nodiscard]] bool stop_requested() const noexcept;
bool request_stop() noexcept;

[[nodiscard]] friend bool
  operator==(const stop_source& lhs, const stop_source& rhs) noexcept;
friend void swap(stop_source& lhs, stop_source& rhs) noexcept;

}; }

32.3.4.1 Constructors, copy, and assignment [stopsource.cons]

Effects:Initialises *this to have ownership of a new stop state.

Postconditions: stop_­possible() is trueand stop_­requested() is false.

Throws: bad_­alloc if memory could not be allocated for the stop state.

explicit stop_source(nostopstate_t) noexcept;

Postconditions: stop_­possible() is false andstop_­requested() is false.

[ Note

:

No resources are allocated for the state.

end note

]

stop_source(const stop_source& rhs) noexcept;

Postconditions: *this == rhs is true.

[ Note

:

*this and rhs share the ownership of the same stop state, if any.

end note

]

stop_source(stop_source&& rhs) noexcept;

Postconditions: *this contains the value of rhsprior to the start of construction and rhs.stop_­possible() is false.

Effects:Releases ownership of the stop state, if any.

stop_source& operator=(const stop_source& rhs) noexcept;

Effects:Equivalent to: stop_­source(rhs).swap(*this).

stop_source& operator=(stop_source&& rhs) noexcept;

Effects:Equivalent to: stop_­source(std​::​move(rhs)).swap(*this).

void swap(stop_source& rhs) noexcept;

Effects:Exchanges the values of *this and rhs.

32.3.4.2 Members [stopsource.mem]

[[nodiscard]] stop_token get_token() const noexcept;

Returns: stop_­token() if stop_­possible() is false; otherwise a new associated stop_­token object.

[[nodiscard]] bool stop_possible() const noexcept;

Returns: true if *this has ownership of a stop state; otherwise, false.

[[nodiscard]] bool stop_requested() const noexcept;

Returns: true if *this has ownership of a stop state that has received a stop request; otherwise, false.

bool request_stop() noexcept;

Effects:If *this does not have ownership of a stop state, returns false.

Otherwise, atomically determines whether the owned stop state has received a stop request, and if not, makes a stop request.

The determination and making of the stop request are an atomic read-modify-write operation ([intro.races]).

If the request was made, the callbacks registered by associated stop_­callback objects are synchronously called.

If an invocation of a callback exits via an exception then terminate is called ([except.terminate]).

[ Note

:

A stop request includes notifying all condition variables of type condition_­variable_­anytemporarily registered during an interruptible wait ([thread.condvarany.intwait]).

end note

]

Postconditions: stop_­possible() is falseor stop_­requested() is true.

Returns: true if this call made a stop request; otherwise false.

32.3.4.3 Non-member functions [stopsource.nonmembers]

[[nodiscard]] friend bool operator==(const stop_source& lhs, const stop_source& rhs) noexcept;

Returns: true if lhs and rhs have ownership of the same stop state or if both lhs and rhs do not have ownership of a stop state; otherwise false.

friend void swap(stop_source& x, stop_source& y) noexcept;

Effects:Equivalent to: x.swap(y).

32.3.5 Class template stop_­callback [stopcallback]

namespace std { template class stop_callback { public: using callback_type = Callback;

template<class C>
explicit stop_callback(const stop_token& st, C&& cb)
    noexcept(is_nothrow_constructible_v<Callback, C>);
template<class C>
explicit stop_callback(stop_token&& st, C&& cb)
    noexcept(is_nothrow_constructible_v<Callback, C>);
~stop_callback();

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

private: Callback callback;
};

template stop_callback(stop_token, Callback) -> stop_callback; }

Mandates: stop_­callback is instantiated with an argument for the template parameter Callbackthat satisfies both invocableand destructible.

Preconditions: stop_­callback is instantiated with an argument for the template parameter Callbackthat models both invocableand destructible.

32.3.5.1 Constructors and destructor [stopcallback.cons]

template<class C> explicit stop_callback(const stop_token& st, C&& cb) noexcept(is_nothrow_constructible_v<Callback, C>);template<class C> explicit stop_callback(stop_token&& st, C&& cb) noexcept(is_nothrow_constructible_v<Callback, C>);

Constraints: Callback and C satisfy constructible_­from<Callback, C>.

Preconditions: Callback and C model constructible_­from<Callback, C>.

Effects:Initializes callback with std​::​forward<C>(cb).

If st.stop_­requested() is true, thenstd​::​forward<Callback>(callback)()is evaluated in the current thread before the constructor returns.

Otherwise, if st has ownership of a stop state, acquires shared ownership of that stop state and registers the callback with that stop state such that std​::​forward<Callback>(callback)()is evaluated by the first call to request_­stop()on an associated stop_­source.

Remarks:If evaluatingstd​::​forward<Callback>(callback)()exits via an exception, then terminate is called ([except.terminate]).

Throws:Any exception thrown by the initialization of callback.

Effects:Unregisters the callback from the owned stop state, if any.

The destructor does not block waiting for the execution of another callback registered by an associated stop_­callback.

If callback is concurrently executing on another thread, then the return from the invocation of callbackstrongly happens before ([intro.races])callback is destroyed.

If callback is executing on the current thread, then the destructor does not block ([defns.block]) waiting for the return from the invocation of callback.

Releases ownership of the stop state, if any.

32.4 Threads [thread.threads]

[thread.threads] describes components that can be used to create and manage threads.

[ Note

:

These threads are intended to map one-to-one with operating system threads.

end note

]

32.4.1 Header synopsis [thread.syn]

#include
#include

namespace std { class thread;

void swap(thread& x, thread& y) noexcept;

class jthread;

namespace this_thread { thread::id get_id() noexcept;

void yield() noexcept;
template<class Clock, class Duration>
  void sleep_until(const chrono::time_point<Clock, Duration>& abs_time);
template<class Rep, class Period>
  void sleep_for(const chrono::duration<Rep, Period>& rel_time);

} }

32.4.2 Class thread [thread.thread.class]

The class thread provides a mechanism to create a new thread of execution, to join with a thread (i.e., wait for a thread to complete), and to perform other operations that manage and query the state of a thread.

A thread object uniquely represents a particular thread of execution.

That representation may be transferred to other thread objects in such a way that no two thread objects simultaneously represent the same thread of execution.

A thread of execution is detached when no thread object represents that thread.

Objects of class thread can be in a state that does not represent a thread of execution.

[ Note

:

A thread object does not represent a thread of execution after default construction, after being moved from, or after a successful call to detach orjoin.

end note

]

namespace std { class thread { public:

class id;
using native_handle_type = implementation-defined;         


thread() noexcept;
template<class F, class... Args> explicit thread(F&& f, Args&&... args);
~thread();
thread(const thread&) = delete;
thread(thread&&) noexcept;
thread& operator=(const thread&) = delete;
thread& operator=(thread&&) noexcept;


void swap(thread&) noexcept;
bool joinable() const noexcept;
void join();
void detach();
id get_id() const noexcept;
native_handle_type native_handle();                         


static unsigned int hardware_concurrency() noexcept;

}; }

32.4.2.1 Class thread​::​id [thread.thread.id]

namespace std { class thread::id { public: id() noexcept; };

bool operator==(thread::id x, thread::id y) noexcept; strong_ordering operator<=>(thread::id x, thread::id y) noexcept;

template<class charT, class traits> basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>& out, thread::id id);

template struct hash; template<> struct hashthread::id; }

An object of type thread​::​id provides a unique identifier for each thread of execution and a single distinct value for all threadobjects that do not represent a thread of execution ([thread.thread.class]).

Each thread of execution has an associated thread​::​id object that is not equal to thethread​::​id object of any other thread of execution and that is not equal to the thread​::​id object of any thread object that does not represent threads of execution.

thread​::​id is a trivially copyable class ([class.prop]).

The library may reuse the value of a thread​::​id of a terminated thread that can no longer be joined.

[ Note

:

Relational operators allow thread​::​id objects to be used as keys in associative containers.

end note

]

Postconditions:The constructed object does not represent a thread of execution.

bool operator==(thread::id x, thread::id y) noexcept;

Returns: true only if x and y represent the same thread of execution or neither x nor y represents a thread of execution.

strong_ordering operator<=>(thread::id x, thread::id y) noexcept;

Let be an unspecified total ordering over thread​::​idas described in [alg.sorting].

Returns: strong_­ordering​::​less if is true.

Otherwise, strong_­ordering​::​greaterif is true.

Otherwise, strong_­ordering​::​equal.

template<class charT, class traits> basic_ostream<charT, traits>& operator<< (basic_ostream<charT, traits>& out, thread::id id);

Effects:Inserts an unspecified text representation of id intoout.

For two objects of type thread​::​id x and y, if x == y the thread​::​id objects have the same text representation and if x != y the thread​::​id objects have distinct text representations.

template<> struct hash<thread::id>;

32.4.2.2 Constructors [thread.thread.constr]

Effects:The object does not represent a thread of execution.

Postconditions: get_­id() == id().

template<class F, class... Args> explicit thread(F&& f, Args&&... args);

Constraints: remove_­cvref_­t<F> is not the same type as thread.

Mandates:The following are all true:

Preconditions: decay_­t<F> and each type in decay_­t<Args> meet theCpp17MoveConstructible requirements.

Effects:The new thread of execution executes

invoke(decay-copy(std::forward(f)), decay-copy(std::forward(args))...)

with the calls todecay-copy being evaluated in the constructing thread.

Any return value from this invocation is ignored.

[ Note

:

This implies that any exceptions not thrown from the invocation of the copy of f will be thrown in the constructing thread, not the new thread.

end note

]

If the invocation of invoke terminates with an uncaught exception,terminate is called.

Synchronization:The completion of the invocation of the constructor synchronizes with the beginning of the invocation of the copy of f.

Postconditions: get_­id() != id().

*this represents the newly started thread.

Throws: system_­error if unable to start the new thread.

Error conditions:

thread(thread&& x) noexcept;

Postconditions: x.get_­id() == id() and get_­id() returns the value of x.get_­id() prior to the start of construction.

32.4.2.3 Destructor [thread.thread.destr]

Effects:If joinable(), calls terminate().

Otherwise, has no effects.

[ Note

:

Either implicitly detaching or joining a joinable() thread in its destructor could result in difficult to debug correctness (for detach) or performance (for join) bugs encountered only when an exception is thrown.

Thus the programmer must ensure that the destructor is never executed while the thread is still joinable.

end note

]

32.4.2.4 Assignment [thread.thread.assign]

thread& operator=(thread&& x) noexcept;

Effects:If joinable(), calls terminate().

Otherwise, assigns the state of x to *this and sets x to a default constructed state.

Postconditions: x.get_­id() == id() and get_­id() returns the value ofx.get_­id() prior to the assignment.

32.4.2.5 Members [thread.thread.member]

void swap(thread& x) noexcept;

Effects:Swaps the state of *this and x.

bool joinable() const noexcept;

Returns: get_­id() != id().

Effects:Blocks until the thread represented by *this has completed.

Synchronization:The completion of the thread represented by *this synchronizes with ([intro.multithread]) the corresponding successfuljoin() return.

[ Note

:

Operations on*this are not synchronized.

end note

]

Postconditions:The thread represented by *this has completed.

get_­id() == id().

Throws: system_­error when an exception is required ([thread.req.exception]).

Error conditions:

Effects:The thread represented by *this continues execution without the calling thread blocking.

When detach() returns, *this no longer represents the possibly continuing thread of execution.

When the thread previously represented by *this ends execution, the implementation releases any owned resources.

Postconditions: get_­id() == id().

Throws: system_­error when an exception is required ([thread.req.exception]).

Error conditions:

id get_id() const noexcept;

Returns:A default constructed id object if *this does not represent a thread, otherwise this_­thread​::​get_­id() for the thread of execution represented by*this.

32.4.2.6 Static members [thread.thread.static]

unsigned hardware_concurrency() noexcept;

Returns:The number of hardware thread contexts.

[ Note

:

This value should only be considered to be a hint.

end note

]

If this value is not computable or well-defined, an implementation should return 0.

32.4.3 Class jthread [thread.jthread.class]

The class jthread provides a mechanism to create a new thread of execution.

The functionality is the same as for class thread ([thread.thread.class]) with the additional abilities to provide a stop_­token ([thread.stoptoken]) to the new thread of execution, make stop requests, and automatically join.

namespace std { class jthread { public:

using id = thread::id;
using native_handle_type = thread::native_handle_type;


jthread() noexcept;
template<class F, class... Args> explicit jthread(F&& f, Args&&... args);
~jthread();
jthread(const jthread&) = delete;
jthread(jthread&&) noexcept;
jthread& operator=(const jthread&) = delete;
jthread& operator=(jthread&&) noexcept;


void swap(jthread&) noexcept;
[[nodiscard]] bool joinable() const noexcept;
void join();
void detach();
[[nodiscard]] id get_id() const noexcept;
[[nodiscard]] native_handle_type native_handle();   


[[nodiscard]] stop_source get_stop_source() noexcept;
[[nodiscard]] stop_token get_stop_token() const noexcept;
bool request_stop() noexcept;


friend void swap(jthread& lhs, jthread& rhs) noexcept;


[[nodiscard]] static unsigned int hardware_concurrency() noexcept;

private: stop_source ssource;
}; }

32.4.3.1 Constructors, move, and assignment [thread.jthread.cons]

Effects:Constructs a jthread object that does not represent a thread of execution.

Postconditions: get_­id() == id() is trueand ssource.stop_­possible() is false.

template<class F, class... Args> explicit jthread(F&& f, Args&&... args);

Constraints: remove_­cvref_­t<F> is not the same type as jthread.

Mandates:The following are all true:

Preconditions: decay_­t<F> and each type in decay_­t<Args> meet theCpp17MoveConstructible requirements.

Effects:Initializes ssource.

The new thread of execution executes

invoke(decay-copy(std::forward(f)), get_stop_token(), decay-copy(std::forward(args))...)

if that expression is well-formed, otherwise

invoke(decay-copy(std::forward(f)), decay-copy(std::forward(args))...)

with the calls todecay-copy being evaluated in the constructing thread.

Any return value from this invocation is ignored.

[ Note

:

This implies that any exceptions not thrown from the invocation of the copy of f will be thrown in the constructing thread, not the new thread.

end note

]

If the invoke expression exits via an exception,terminate is called.

Synchronization:The completion of the invocation of the constructor synchronizes with the beginning of the invocation of the copy of f.

Postconditions: get_­id() != id() is trueand ssource.stop_­possible() is trueand *this represents the newly started thread.

[ Note

:

The calling thread can make a stop request only once, because it cannot replace this stop token.

end note

]

Throws: system_­error if unable to start the new thread.

Error conditions:

jthread(jthread&& x) noexcept;

Postconditions: x.get_­id() == id()and get_­id() returns the value of x.get_­id()prior to the start of construction.

ssource has the value of x.ssourceprior to the start of construction and x.ssource.stop_­possible() is false.

Effects:If joinable() is true, calls request_­stop() and then join().

[ Note

:

Operations on *this are not synchronized.

end note

]

jthread& operator=(jthread&& x) noexcept;

Effects:If joinable() is true, calls request_­stop() and then join().

Assigns the state of x to *thisand sets x to a default constructed state.

Postconditions: x.get_­id() == id()and get_­id() returns the value of x.get_­id()prior to the assignment.

ssource has the value of x.ssourceprior to the assignment and x.ssource.stop_­possible() is false.

32.4.3.2 Members [thread.jthread.mem]

void swap(jthread& x) noexcept;

Effects:Exchanges the values of *this and x.

[[nodiscard]] bool joinable() const noexcept;

Returns: get_­id() != id().

Effects:Blocks until the thread represented by *this has completed.

Synchronization:The completion of the thread represented by *thissynchronizes with ([intro.multithread]) the corresponding successful join() return.

[ Note

:

Operations on *this are not synchronized.

end note

]

Postconditions:The thread represented by *this has completed.

get_­id() == id().

Throws: system_­error when an exception is required ([thread.req.exception]).

Error conditions:

Effects:The thread represented by *this continues execution without the calling thread blocking.

When detach() returns,*this no longer represents the possibly continuing thread of execution.

When the thread previously represented by *this ends execution, the implementation releases any owned resources.

Postconditions: get_­id() == id().

Throws: system_­error when an exception is required ([thread.req.exception]).

Error conditions:

id get_id() const noexcept;

Returns:A default constructed id object if *this does not represent a thread, otherwise this_­thread​::​get_­id()for the thread of execution represented by *this.

32.4.3.3 Stop token handling [thread.jthread.stop]

[[nodiscard]] stop_source get_stop_source() noexcept;

Effects:Equivalent to: return ssource;

[[nodiscard]] stop_token get_stop_token() const noexcept;

Effects:Equivalent to: return ssource.get_­token();

bool request_stop() noexcept;

Effects:Equivalent to: return ssource.request_­stop();

32.4.3.4 Specialized algorithms [thread.jthread.special]

friend void swap(jthread& x, jthread& y) noexcept;

Effects:Equivalent to: x.swap(y).

32.4.3.5 Static members [thread.jthread.static]

[[nodiscard]] static unsigned int hardware_concurrency() noexcept;

Returns: thread​::​hardware_­concurrency().

32.4.4 Namespace this_­thread [thread.thread.this]

namespace std::this_thread { thread::id get_id() noexcept;

void yield() noexcept; template<class Clock, class Duration> void sleep_until(const chrono::time_point<Clock, Duration>& abs_time); template<class Rep, class Period> void sleep_for(const chrono::duration<Rep, Period>& rel_time); }

thread::id this_thread::get_id() noexcept;

Returns:An object of type thread​::​id that uniquely identifies the current thread of execution.

No other thread of execution has this id and this thread of execution always has this id.

The object returned does not compare equal to a default constructedthread​::​id.

void this_thread::yield() noexcept;

Effects:Offers the implementation the opportunity to reschedule.

template<class Clock, class Duration> void sleep_until(const chrono::time_point<Clock, Duration>& abs_time);

Effects:Blocks the calling thread for the absolute timeout ([thread.req.timing]) specified by abs_­time.

Throws:Timeout-related exceptions ([thread.req.timing]).

template<class Rep, class Period> void sleep_for(const chrono::duration<Rep, Period>& rel_time);

Effects:Blocks the calling thread for the relative timeout ([thread.req.timing]) specified by rel_­time.

Throws:Timeout-related exceptions ([thread.req.timing]).

32.5 Mutual exclusion [thread.mutex]

This subclause provides mechanisms for mutual exclusion: mutexes, locks, and call once.

These mechanisms ease the production of race-free programs ([intro.multithread]).

32.5.1 Header synopsis [mutex.syn]

namespace std { class mutex; class recursive_mutex; class timed_mutex; class recursive_timed_mutex;

struct defer_lock_t { explicit defer_lock_t() = default; }; struct try_to_lock_t { explicit try_to_lock_t() = default; }; struct adopt_lock_t { explicit adopt_lock_t() = default; };

inline constexpr defer_lock_t defer_lock { }; inline constexpr try_to_lock_t try_to_lock { }; inline constexpr adopt_lock_t adopt_lock { };

template class lock_guard; template<class... MutexTypes> class scoped_lock; template class unique_lock;

template void swap(unique_lock& x, unique_lock& y) noexcept;

template<class L1, class L2, class... L3> int try_lock(L1&, L2&, L3&...); template<class L1, class L2, class... L3> void lock(L1&, L2&, L3&...);

struct once_flag;

template<class Callable, class... Args> void call_once(once_flag& flag, Callable&& func, Args&&... args); }

32.5.3 Mutex requirements [thread.mutex.requirements]

32.5.3.1 In general [thread.mutex.requirements.general]

A mutex object facilitates protection against data races and allows safe synchronization of data between execution agents.

An execution agent owns a mutex from the time it successfully calls one of the lock functions until it calls unlock.

Mutexes can be either recursive or non-recursive, and can grant simultaneous ownership to one or many execution agents.

Both recursive and non-recursive mutexes are supplied.

32.5.3.2 Mutex types [thread.mutex.requirements.mutex]

The mutex types are the standard library types mutex,recursive_­mutex, timed_­mutex, recursive_­timed_­mutex,shared_­mutex, and shared_­timed_­mutex.

They meet the requirements set out in this subclause.

In this description, mdenotes an object of a mutex type.

The mutex types meet the Cpp17Lockable requirements ([thread.req.lockable.req]).

The mutex types meet Cpp17DefaultConstructible and Cpp17Destructible.

If initialization of an object of a mutex type fails, an exception of type system_­error is thrown.

The mutex types are neither copyable nor movable.

The error conditions for error codes, if any, reported by member functions of the mutex types are as follows:

The implementation provides lock and unlock operations, as described below.

For purposes of determining the existence of a data race, these behave as atomic operations ([intro.multithread]).

The lock and unlock operations on a single mutex appears to occur in a single total order.

[ Note

:

Construction and destruction of an object of a mutex type need not be thread-safe; other synchronization should be used to ensure that mutex objects are initialized and visible to other threads.

end note

]

The expression m.lock() is well-formed and has the following semantics:

Preconditions:If m is of type mutex, timed_­mutex,shared_­mutex, or shared_­timed_­mutex, the calling thread does not own the mutex.

Effects:Blocks the calling thread until ownership of the mutex can be obtained for the calling thread.

Postconditions:The calling thread owns the mutex.

Throws: system_­error when an exception is required ([thread.req.exception]).

Error conditions:

The expression m.try_­lock() is well-formed and has the following semantics:

Preconditions:If m is of type mutex, timed_­mutex,shared_­mutex, or shared_­timed_­mutex, the calling thread does not own the mutex.

Effects:Attempts to obtain ownership of the mutex for the calling thread without blocking.

If ownership is not obtained, there is no effect and try_­lock()immediately returns.

An implementation may fail to obtain the lock even if it is not held by any other thread.

[ Note

:

This spurious failure is normally uncommon, but allows interesting implementations based on a simple compare and exchange ([atomics]).

end note

]

An implementation should ensure that try_­lock() does not consistently return falsein the absence of contending mutex acquisitions.

Returns: true if ownership of the mutex was obtained for the calling thread, otherwise false.

Synchronization:If try_­lock() returns true, prior unlock() operations on the same object synchronize with this operation.

[ Note

:

Since lock() does not synchronize with a failed subsequenttry_­lock(), the visibility rules are weak enough that little would be known about the state after a failure, even in the absence of spurious failures.

end note

]

The expression m.unlock() is well-formed and has the following semantics:

Preconditions:The calling thread owns the mutex.

Effects:Releases the calling thread's ownership of the mutex.

Synchronization:This operation synchronizes with subsequent lock operations that obtain ownership on the same object.

32.5.3.2.1 Class mutex [thread.mutex.class]

namespace std { class mutex { public: constexpr mutex() noexcept; ~mutex();

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

void lock();
bool try_lock();
void unlock();

using native_handle_type = implementation-defined;          
native_handle_type native_handle();                         

}; }

The class mutex provides a non-recursive mutex with exclusive ownership semantics.

If one thread owns a mutex object, attempts by another thread to acquire ownership of that object will fail (for try_­lock()) or block (forlock()) until the owning thread has released ownership with a call tounlock().

[ Note

:

After a thread A has called unlock(), releasing a mutex, it is possible for another thread B to lock the same mutex, observe that it is no longer in use, unlock it, and destroy it, before thread A appears to have returned from its unlock call.

Implementations are required to handle such scenarios correctly, as long as thread A doesn't access the mutex after the unlock call returns.

These cases typically occur when a reference-counted object contains a mutex that is used to protect the reference count.

end note

]

The class mutex meets all of the mutex requirements ([thread.mutex.requirements]).

[ Note

:

A program can deadlock if the thread that owns a mutex object callslock() on that object.

If the implementation can detect the deadlock, a resource_­deadlock_­would_­occur error condition might be observed.

end note

]

The behavior of a program is undefined if it destroys a mutex object owned by any thread or a thread terminates while owning a mutex object.

32.5.3.2.2 Class recursive_­mutex [thread.mutex.recursive]

namespace std { class recursive_mutex { public: recursive_mutex(); ~recursive_mutex();

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

void lock();
bool try_lock() noexcept;
void unlock();

using native_handle_type = implementation-defined;          
native_handle_type native_handle();                         

}; }

The class recursive_­mutex provides a recursive mutex with exclusive ownership semantics.

If one thread owns a recursive_­mutex object, attempts by another thread to acquire ownership of that object will fail (for try_­lock()) or block (for lock()) until the first thread has completely released ownership.

The class recursive_­mutex meets all of the mutex requirements ([thread.mutex.requirements]).

A thread that owns a recursive_­mutex object may acquire additional levels of ownership by calling lock() or try_­lock() on that object.

It is unspecified how many levels of ownership may be acquired by a single thread.

If a thread has already acquired the maximum level of ownership for a recursive_­mutexobject, additional calls to try_­lock() fail, and additional calls tolock() throw an exception of type system_­error.

A thread shall call unlock() once for each level of ownership acquired by calls tolock() and try_­lock().

Only when all levels of ownership have been released may ownership be acquired by another thread.

The behavior of a program is undefined if:

32.5.3.3 Timed mutex types [thread.timedmutex.requirements]

The timed mutex types are the standard library types timed_­mutex,recursive_­timed_­mutex, and shared_­timed_­mutex.

They meet the requirements set out below.

In this description, m denotes an object of a mutex type,rel_­time denotes an object of an instantiation of duration, and abs_­time denotes an object of an instantiation of time_­point.

The timed mutex types meet the Cpp17TimedLockablerequirements ([thread.req.lockable.timed]).

The expression m.try_­lock_­for(rel_­time) is well-formed and has the following semantics:

Preconditions:If m is of type timed_­mutex orshared_­timed_­mutex, the calling thread does not own the mutex.

Effects:The function attempts to obtain ownership of the mutex within the relative timeout ([thread.req.timing]) specified by rel_­time.

If the time specified by rel_­time is less than or equal to rel_­time.zero(), the function attempts to obtain ownership without blocking (as if by callingtry_­lock()).

The function returns within the timeout specified byrel_­time only if it has obtained ownership of the mutex object.

[ Note

:

As with try_­lock(), there is no guarantee that ownership will be obtained if the lock is available, but implementations are expected to make a strong effort to do so.

end note

]

Returns: true if ownership was obtained, otherwise false.

Synchronization:If try_­lock_­for() returns true, prior unlock() operations on the same object synchronize with this operation.

Throws:Timeout-related exceptions ([thread.req.timing]).

The expression m.try_­lock_­until(abs_­time) is well-formed and has the following semantics:

Preconditions:If m is of type timed_­mutex orshared_­timed_­mutex, the calling thread does not own the mutex.

Effects:The function attempts to obtain ownership of the mutex.

Ifabs_­time has already passed, the function attempts to obtain ownership without blocking (as if by calling try_­lock()).

The function returns before the absolute timeout ([thread.req.timing]) specified byabs_­time only if it has obtained ownership of the mutex object.

[ Note

:

As with try_­lock(), there is no guarantee that ownership will be obtained if the lock is available, but implementations are expected to make a strong effort to do so.

end note

]

Returns: true if ownership was obtained, otherwise false.

Synchronization:If try_­lock_­until() returns true, prior unlock()operations on the same object synchronize withthis operation.

Throws:Timeout-related exceptions ([thread.req.timing]).

32.5.3.3.1 Class timed_­mutex [thread.timedmutex.class]

namespace std { class timed_mutex { public: timed_mutex(); ~timed_mutex();

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

void lock();    
bool try_lock();
template<class Rep, class Period>
  bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
template<class Clock, class Duration>
  bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
void unlock();

using native_handle_type = implementation-defined;          
native_handle_type native_handle();                         

}; }

The class timed_­mutex provides a non-recursive mutex with exclusive ownership semantics.

If one thread owns a timed_­mutex object, attempts by another thread to acquire ownership of that object will fail (for try_­lock()) or block (for lock(), try_­lock_­for(), and try_­lock_­until()) until the owning thread has released ownership with a call to unlock() or the call to try_­lock_­for() or try_­lock_­until() times out (having failed to obtain ownership).

The class timed_­mutex meets all of the timed mutex requirements ([thread.timedmutex.requirements]).

The behavior of a program is undefined if:

32.5.3.3.2 Class recursive_­timed_­mutex [thread.timedmutex.recursive]

namespace std { class recursive_timed_mutex { public: recursive_timed_mutex(); ~recursive_timed_mutex();

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

void lock();    
bool try_lock() noexcept;
template<class Rep, class Period>
  bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
template<class Clock, class Duration>
  bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
void unlock();

using native_handle_type = implementation-defined;          
native_handle_type native_handle();                         

}; }

The class recursive_­timed_­mutex provides a recursive mutex with exclusive ownership semantics.

If one thread owns a recursive_­timed_­mutex object, attempts by another thread to acquire ownership of that object will fail (fortry_­lock()) or block (for lock(), try_­lock_­for(), andtry_­lock_­until()) until the owning thread has completely released ownership or the call to try_­lock_­for() or try_­lock_­until()times out (having failed to obtain ownership).

The class recursive_­timed_­mutex meets all of the timed mutex requirements ([thread.timedmutex.requirements]).

A thread that owns a recursive_­timed_­mutex object may acquire additional levels of ownership by calling lock(), try_­lock(),try_­lock_­for(), or try_­lock_­until() on that object.

It is unspecified how many levels of ownership may be acquired by a single thread.

If a thread has already acquired the maximum level of ownership for arecursive_­timed_­mutex object, additional calls to try_­lock(),try_­lock_­for(), or try_­lock_­until() fail, and additional calls to lock() throw an exception of type system_­error.

A thread shall call unlock() once for each level of ownership acquired by calls to lock(), try_­lock(), try_­lock_­for(), andtry_­lock_­until().

Only when all levels of ownership have been released may ownership of the object be acquired by another thread.

The behavior of a program is undefined if:

32.5.4 Locks [thread.lock]

A lock is an object that holds a reference to a lockable object and may unlock the lockable object during the lock's destruction (such as when leaving block scope).

An execution agent may use a lock to aid in managing ownership of a lockable object in an exception safe manner.

A lock is said to own a lockable object if it is currently managing the ownership of that lockable object for an execution agent.

A lock does not manage the lifetime of the lockable object it references.

[ Note

:

Locks are intended to ease the burden of unlocking the lockable object under both normal and exceptional circumstances.

end note

]

Some lock constructors take tag types which describe what should be done with the lockable object during the lock's construction.

namespace std { struct defer_lock_t { };
struct try_to_lock_t { };

struct adopt_lock_t { };

inline constexpr defer_lock_t defer_lock { }; inline constexpr try_to_lock_t try_to_lock { }; inline constexpr adopt_lock_t adopt_lock { }; }

32.5.4.1 Class template lock_­guard [thread.lock.guard]

namespace std { template class lock_guard { public: using mutex_type = Mutex;

explicit lock_guard(mutex_type& m);
lock_guard(mutex_type& m, adopt_lock_t);
~lock_guard();

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

private: mutex_type& pm;
}; }

An object of type lock_­guard controls the ownership of a lockable object within a scope.

A lock_­guard object maintains ownership of a lockable object throughout the lock_­guard object's lifetime.

The behavior of a program is undefined if the lockable object referenced bypm does not exist for the entire lifetime of the lock_­guardobject.

The supplied Mutex type shall meet the Cpp17BasicLockablerequirements ([thread.req.lockable.basic]).

explicit lock_guard(mutex_type& m);

Preconditions:If mutex_­type is not a recursive mutex, the calling thread does not own the mutex m.

Effects:Initializes pm with m.

Calls m.lock().

lock_guard(mutex_type& m, adopt_lock_t);

Preconditions:The calling thread owns the mutex m.

Effects:Initializes pm with m.

Effects:As if by pm.unlock().

32.5.4.2 Class template scoped_­lock [thread.lock.scoped]

namespace std { template<class... MutexTypes> class scoped_lock { public: using mutex_type = Mutex;

explicit scoped_lock(MutexTypes&... m);
explicit scoped_lock(adopt_lock_t, MutexTypes&... m);
~scoped_lock();

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

private: tuple<MutexTypes&...> pm;
}; }

An object of type scoped_­lock controls the ownership of lockable objects within a scope.

A scoped_­lock object maintains ownership of lockable objects throughout the scoped_­lock object's lifetime.

The behavior of a program is undefined if the lockable objects referenced bypm do not exist for the entire lifetime of the scoped_­lockobject.

When sizeof...(MutexTypes) is 1, the supplied Mutex type shall meet the Cpp17BasicLockable requirements ([thread.req.lockable.basic]).

Otherwise, each of the mutex types shall meet the Cpp17Lockable requirements ([thread.req.lockable.req]).

explicit scoped_lock(MutexTypes&... m);

Preconditions:If a MutexTypes type is not a recursive mutex, the calling thread does not own the corresponding mutex element of m.

Effects:Initializes pm with tie(m...).

Then if sizeof...(MutexTypes) is 0, no effects.

Otherwise if sizeof...(MutexTypes) is 1, then m.lock().

Otherwise, lock(m...).

explicit scoped_lock(adopt_lock_t, MutexTypes&... m);

Preconditions:The calling thread owns all the mutexes in m.

Effects:Initializes pm with tie(m...).

Effects:For all i in [0, sizeof...(MutexTypes)),get<i>(pm).unlock().

32.5.4.3 Class template unique_­lock [thread.lock.unique]

namespace std { template class unique_lock { public: using mutex_type = Mutex;

unique_lock() noexcept;
explicit unique_lock(mutex_type& m);
unique_lock(mutex_type& m, defer_lock_t) noexcept;
unique_lock(mutex_type& m, try_to_lock_t);
unique_lock(mutex_type& m, adopt_lock_t);
template<class Clock, class Duration>
  unique_lock(mutex_type& m, const chrono::time_point<Clock, Duration>& abs_time);
template<class Rep, class Period>
  unique_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time);
~unique_lock();

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

unique_lock(unique_lock&& u) noexcept;
unique_lock& operator=(unique_lock&& u);


void lock();
bool try_lock();

template<class Rep, class Period>
  bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
template<class Clock, class Duration>
  bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);

void unlock();


void swap(unique_lock& u) noexcept;
mutex_type* release() noexcept;


bool owns_lock() const noexcept;
explicit operator bool () const noexcept;
mutex_type* mutex() const noexcept;

private: mutex_type* pm;
bool owns;
};

template void swap(unique_lock& x, unique_lock& y) noexcept; }

An object of type unique_­lock controls the ownership of a lockable object within a scope.

Ownership of the lockable object may be acquired at construction or after construction, and may be transferred, after acquisition, to another unique_­lock object.

Objects of type unique_­lock are not copyable but are movable.

The behavior of a program is undefined if the contained pointerpm is not null and the lockable object pointed to by pm does not exist for the entire remaining lifetime ([basic.life]) of the unique_­lock object.

The suppliedMutex type shall meet the Cpp17BasicLockablerequirements ([thread.req.lockable.basic]).

[ Note

:

unique_­lock<Mutex> meets the Cpp17BasicLockable requirements.

If Mutexmeets the Cpp17Lockable requirements ([thread.req.lockable.req]),unique_­lock<Mutex> also meets the Cpp17Lockable requirements; if Mutexmeets the Cpp17TimedLockable requirements ([thread.req.lockable.timed]),unique_­lock<Mutex> also meets the Cpp17TimedLockable requirements.

end note

]

32.5.4.3.1 Constructors, destructor, and assignment [thread.lock.unique.cons]

Postconditions: pm == 0 and owns == false.

explicit unique_lock(mutex_type& m);

Preconditions:If mutex_­type is not a recursive mutex the calling thread does not own the mutex.

Postconditions: pm == addressof(m) and owns == true.

unique_lock(mutex_type& m, defer_lock_t) noexcept;

Postconditions: pm == addressof(m) and owns == false.

unique_lock(mutex_type& m, try_to_lock_t);

Preconditions:The supplied Mutex type meets the Cpp17Lockablerequirements ([thread.req.lockable.req]).

If mutex_­type is not a recursive mutex the calling thread does not own the mutex.

Effects:Calls m.try_­lock().

Postconditions: pm == addressof(m) and owns == res, where res is the value returned by the call to m.try_­lock().

unique_lock(mutex_type& m, adopt_lock_t);

Preconditions:The calling thread owns the mutex.

Postconditions: pm == addressof(m) and owns == true.

template<class Clock, class Duration> unique_lock(mutex_type& m, const chrono::time_point<Clock, Duration>& abs_time);

Preconditions:If mutex_­type is not a recursive mutex the calling thread does not own the mutex.

The supplied Mutex type meets theCpp17TimedLockable requirements ([thread.req.lockable.timed]).

Effects:Calls m.try_­lock_­until(abs_­time).

Postconditions: pm == addressof(m) and owns == res, where res is the value returned by the call to m.try_­lock_­until(abs_­time).

template<class Rep, class Period> unique_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time);

Preconditions:If mutex_­type is not a recursive mutex the calling thread does not own the mutex.

The supplied Mutex type meets the Cpp17TimedLockable requirements ([thread.req.lockable.timed]).

Effects:Calls m.try_­lock_­for(rel_­time).

Postconditions: pm == addressof(m) and owns == res, where res is the value returned by the call to m.try_­lock_­for(rel_­time).

unique_lock(unique_lock&& u) noexcept;

Postconditions: pm == u_­p.pm and owns == u_­p.owns (where u_­p is the state of u just prior to this construction), u.pm == 0 and u.owns == false.

unique_lock& operator=(unique_lock&& u);

Effects:If owns calls pm->unlock().

Postconditions: pm == u_­p.pm and owns == u_­p.owns (where u_­p is the state of u just prior to this construction), u.pm == 0 and u.owns == false.

[ Note

:

With a recursive mutex it is possible for both *this and u to own the same mutex before the assignment.

In this case, *this will own the mutex after the assignment and u will not.

end note

]

Effects:If owns calls pm->unlock().

32.5.4.3.2 Locking [thread.lock.unique.locking]

Effects:As if by pm->lock().

Postconditions: owns == true.

Throws:Any exception thrown by pm->lock().

system_­error when an exception is required ([thread.req.exception]).

Error conditions:

Preconditions:The supplied Mutex meets the Cpp17Lockablerequirements ([thread.req.lockable.req]).

Effects:As if by pm->try_­lock().

Returns:The value returned by the call to try_­lock().

Postconditions: owns == res, where res is the value returned by the call to try_­lock().

Throws:Any exception thrown by pm->try_­lock().

system_­error when an exception is required ([thread.req.exception]).

Error conditions:

template<class Clock, class Duration> bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);

Preconditions:The supplied Mutex type meets the Cpp17TimedLockablerequirements ([thread.req.lockable.timed]).

Effects:As if by pm->try_­lock_­until(abs_­time).

Returns:The value returned by the call to try_­lock_­until(abs_­time).

Postconditions: owns == res, where res is the value returned by the call to try_­lock_­until(abs_­time).

Throws:Any exception thrown by pm->try_­lock_­until().

system_­error when an exception is required ([thread.req.exception]).

Error conditions:

template<class Rep, class Period> bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);

Preconditions:The supplied Mutex type meets the Cpp17TimedLockable requirements ([thread.req.lockable.timed]).

Effects:As if by pm->try_­lock_­for(rel_­time).

Returns:The value returned by the call to try_­lock_­for(rel_­time).

Postconditions: owns == res, where res is the value returned by the call to try_­lock_­for(rel_­time).

Throws:Any exception thrown by pm->try_­lock_­for().

system_­error when an exception is required ([thread.req.exception]).

Error conditions:

Effects:As if by pm->unlock().

Postconditions: owns == false.

Throws: system_­error when an exception is required ([thread.req.exception]).

Error conditions:

32.5.4.3.3 Modifiers [thread.lock.unique.mod]

void swap(unique_lock& u) noexcept;

Effects:Swaps the data members of *this and u.

mutex_type* release() noexcept;

Returns:The previous value of pm.

Postconditions: pm == 0 and owns == false.

template<class Mutex> void swap(unique_lock<Mutex>& x, unique_lock<Mutex>& y) noexcept;

Effects:As if by x.swap(y).

32.5.4.3.4 Observers [thread.lock.unique.obs]

bool owns_lock() const noexcept;

explicit operator bool() const noexcept;

mutex_type *mutex() const noexcept;

32.5.5 Generic locking algorithms [thread.lock.algorithm]

template<class L1, class L2, class... L3> int try_lock(L1&, L2&, L3&...);

Preconditions:Each template parameter type meets the Cpp17Lockable requirements.

[ Note

:

Theunique_­lock class template meets these requirements when suitably instantiated.

end note

]

Effects:Calls try_­lock() for each argument in order beginning with the first until all arguments have been processed or a call to try_­lock() fails, either by returning false or by throwing an exception.

If a call totry_­lock() fails, unlock() is called for all prior arguments with no further calls to try_­lock().

Returns: -1 if all calls to try_­lock() returned true, otherwise a zero-based index value that indicates the argument for which try_­lock()returned false.

template<class L1, class L2, class... L3> void lock(L1&, L2&, L3&...);

Preconditions:Each template parameter type meets the Cpp17Lockable requirements.

[ Note

:

Theunique_­lock class template meets these requirements when suitably instantiated.

end note

]

Effects:All arguments are locked via a sequence of calls to lock(),try_­lock(), or unlock() on each argument.

The sequence of calls does not result in deadlock, but is otherwise unspecified.

[ Note

:

A deadlock avoidance algorithm such as try-and-back-off must be used, but the specific algorithm is not specified to avoid over-constraining implementations.

end note

]

If a call tolock() or try_­lock() throws an exception, unlock() is called for any argument that had been locked by a call to lock() ortry_­lock().

32.5.6 Call once [thread.once]

32.5.6.1 Struct once_­flag [thread.once.onceflag]

namespace std { struct once_flag { constexpr once_flag() noexcept;

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

}; }

The class once_­flag is an opaque data structure that call_­once uses to initialize data without causing a data race or deadlock.

constexpr once_flag() noexcept;

Synchronization:The construction of a once_­flag object is not synchronized.

Postconditions:The object's internal state is set to indicate to an invocation ofcall_­once with the object as its initial argument that no function has been called.

32.5.6.2 Function call_­once [thread.once.callonce]

template<class Callable, class... Args> void call_once(once_flag& flag, Callable&& func, Args&&... args);

Mandates: is_­invocable_­v<Callable, Args...> is true.

Effects:An execution of call_­once that does not call its func is apassive execution.

An execution of call_­once that calls its funcis an active execution.

An active execution callsINVOKE(​std​::​forward<Callable>(func), std​::​forward<Args>(args)...).

If such a call to functhrows an exception the execution is exceptional, otherwise it is returning.

An exceptional execution propagates the exception to the caller ofcall_­once.

Among all executions of call_­once for any givenonce_­flag: at most one is a returning execution; if there is a returning execution, it is the last active execution; and there are passive executions only if there is a returning execution.

[ Note

:

Passive executions allow other threads to reliably observe the results produced by the earlier returning execution.

end note

]

Synchronization:For any given once_­flag: all active executions occur in a total order; completion of an active execution synchronizes withthe start of the next one in this total order; and the returning execution synchronizes with the return from all passive executions.

Throws: system_­error when an exception is required ([thread.req.exception]), or any exception thrown by func.

[ Example

:

void init(); std::once_flag flag;

void f() { std::call_once(flag, init); }

struct initializer { void operator()(); };

void g() { static std::once_flag flag2; std::call_once(flag2, initializer()); }

class information { std::once_flag verified; void verifier(); public: void verify() { std::call_once(verified, &information::verifier, *this); } };

end example

]

32.6 Condition variables [thread.condition]

Condition variables provide synchronization primitives used to block a thread until notified by some other thread that some condition is met or until a system time is reached.

Class condition_­variable provides a condition variable that can only wait on an object of type unique_­lock<mutex>, allowing the implementation to be more efficient.

Class condition_­variable_­any provides a general condition variable that can wait on objects of user-supplied lock types.

Condition variables permit concurrent invocation of the wait, wait_­for,wait_­until, notify_­one and notify_­all member functions.

The executions of notify_­one and notify_­allare atomic.

The executions of wait, wait_­for, and wait_­until are performed in three atomic parts:

  1. 1.the release of the mutex and entry into the waiting state;
  2. 2.the unblocking of the wait; and
  3. 3.the reacquisition of the lock.

The implementation behaves as if all executions of notify_­one, notify_­all, and each part of the wait, wait_­for, and wait_­until executions are executed in a single unspecified total order consistent with the "happens before" order.

Condition variable construction and destruction need not be synchronized.

32.6.1 Header <condition_­variable> synopsis [condition.variable.syn]

namespace std { class condition_variable; class condition_variable_any;

void notify_all_at_thread_exit(condition_variable& cond, unique_lock lk);

enum class cv_status { no_timeout, timeout }; }

32.6.2 Non-member functions [thread.condition.nonmember]

void notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk);

Preconditions: lk is locked by the calling thread and either

Effects:Transfers ownership of the lock associated with lk into internal storage and schedules cond to be notified when the current thread exits, after all objects of thread storage duration associated with the current thread have been destroyed.

This notification is equivalent to:

lk.unlock(); cond.notify_all();

Synchronization:The implied lk.unlock() call is sequenced after the destruction of all objects with thread storage duration associated with the current thread.

[ Note

:

The supplied lock will be held until the thread exits, and care should be taken to ensure that this does not cause deadlock due to lock ordering issues.

After calling notify_­all_­at_­thread_­exit it is recommended that the thread should be exited as soon as possible, and that no blocking or time-consuming tasks are run on that thread.

end note

]

[ Note

:

It is the user's responsibility to ensure that waiting threads do not erroneously assume that the thread has finished if they experience spurious wakeups.

This typically requires that the condition being waited for is satisfied while holding the lock on lk, and that this lock is not released and reacquired prior to calling notify_­all_­at_­thread_­exit.

end note

]

32.6.3 Class condition_­variable [thread.condition.condvar]

namespace std { class condition_variable { public: condition_variable(); ~condition_variable();

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

void notify_one() noexcept;
void notify_all() noexcept;
void wait(unique_lock<mutex>& lock);
template<class Predicate>
  void wait(unique_lock<mutex>& lock, Predicate pred);
template<class Clock, class Duration>
  cv_status wait_until(unique_lock<mutex>& lock,
                       const chrono::time_point<Clock, Duration>& abs_time);
template<class Clock, class Duration, class Predicate>
  bool wait_until(unique_lock<mutex>& lock,
                  const chrono::time_point<Clock, Duration>& abs_time,
                  Predicate pred);
template<class Rep, class Period>
  cv_status wait_for(unique_lock<mutex>& lock,
                     const chrono::duration<Rep, Period>& rel_time);
template<class Rep, class Period, class Predicate>
  bool wait_for(unique_lock<mutex>& lock,
                const chrono::duration<Rep, Period>& rel_time,
                Predicate pred);

using native_handle_type = implementation-defined;          
native_handle_type native_handle();                         

}; }

The class condition_­variable is a standard-layout class ([class.prop]).

Throws: system_­error when an exception is required ([thread.req.exception]).

Error conditions:

Preconditions:There is no thread blocked on *this.

[ Note

:

That is, all threads have been notified; they could 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

]

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.

void wait(unique_lock<mutex>& lock);

Preconditions: lock.owns_­lock() is true and lock.mutex()is locked by the calling thread, and either

Effects:

Remarks:If the function fails to meet the postcondition, terminate()is called ([except.terminate]).

[ Note

:

This can happen if the re-locking of the mutex throws an exception.

end note

]

Postconditions: lock.owns_­lock() is true and lock.mutex()is locked by the calling thread.

template<class Predicate> void wait(unique_lock<mutex>& lock, Predicate pred);

Preconditions: lock.owns_­lock() is true and lock.mutex() is locked by the calling thread, and either

Effects:Equivalent to:

while (!pred()) wait(lock);

Remarks:If the function fails to meet the postcondition, terminate()is called ([except.terminate]).

[ Note

:

This can happen if the re-locking of the mutex throws an exception.

end note

]

Postconditions: lock.owns_­lock() is true and lock.mutex()is locked by the calling thread.

Throws:Any exception thrown by pred.

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

Preconditions: lock.owns_­lock() is true and lock.mutex()is locked by the calling thread, and either

Effects:

Remarks:If the function fails to meet the postcondition, terminate()is called ([except.terminate]).

[ Note

:

This can happen if the re-locking of the mutex throws an exception.

end note

]

Postconditions: lock.owns_­lock() is true and lock.mutex()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 Rep, class Period> cv_status wait_for(unique_lock<mutex>& lock,const chrono::duration<Rep, Period>& rel_time);

Preconditions: lock.owns_­lock() is true and lock.mutex()is locked by the calling thread, and either

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()is called ([except.terminate]).

[ Note

:

This can happen if the re-locking of the mutex throws an exception.

end note

]

Postconditions: lock.owns_­lock() is true and lock.mutex()is locked by the calling thread.

Throws:Timeout-related exceptions ([thread.req.timing]).

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

Preconditions: lock.owns_­lock() is true and lock.mutex() is locked by the calling thread, and either

Effects:Equivalent to:

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

Remarks:If the function fails to meet the postcondition, terminate()is called ([except.terminate]).

[ Note

:

This can happen if the re-locking of the mutex throws an exception.

end note

]

Postconditions: lock.owns_­lock() is true and lock.mutex()is locked by the calling thread.

[ Note

:

The returned value indicates whether the predicate evaluated totrue regardless of whether the timeout was triggered.

end note

]

Throws:Timeout-related exceptions ([thread.req.timing]) or any exception thrown by pred.

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

Preconditions: lock.owns_­lock() is true and lock.mutex()is locked by the calling thread, and either

Effects:Equivalent to:

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

[ Note

:

There is no blocking if pred() is initially true, even if the timeout has already expired.

end note

]

Remarks:If the function fails to meet the postcondition, terminate()is called ([except.terminate]).

[ Note

:

This can happen if the re-locking of the mutex throws an exception.

end note

]

Postconditions: lock.owns_­lock() is true and lock.mutex()is locked by the calling thread.

[ Note

:

The returned value indicates whether the predicate evaluates to trueregardless of whether the timeout was triggered.

end note

]

Throws:Timeout-related exceptions ([thread.req.timing]) or any exception thrown by pred.

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

A Lock type shall meet the Cpp17BasicLockablerequirements ([thread.req.lockable.basic]).

[ 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);


template<class Lock, class Predicate>
  bool wait(Lock& lock, stop_token stoken, Predicate pred);
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);
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);

}; }

condition_variable_any();

Throws: bad_­alloc or system_­error when an exception is required ([thread.req.exception]).

Error conditions:

~condition_variable_any();

Preconditions:There is no thread blocked on *this.

[ Note

:

That is, all threads have been notified; they could 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

]

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.

32.6.4.1 Noninterruptible waits [thread.condvarany.wait]

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

Effects:

Remarks:If the function fails to meet the postcondition, terminate()is 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.

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:

Remarks:If the function fails to meet the postcondition, terminate()is 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()is 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 trueregardless 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));

32.6.4.2 Interruptible waits [thread.condvarany.intwait]

The following wait functions will be notified when there is a stop request on the passed stop_­token.

In that case the functions return immediately, returning false if the predicate evaluates to false.

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

Effects:Registers for the duration of this call *thisto get notified on a stop request on stokenduring this call and then equivalent to:

while (!stoken.stop_requested()) { if (pred()) return true; wait(lock); } return pred();

[ Note

:

The returned value indicates whether the predicate evaluated totrue regardless of whether there was a stop request.

end note

]

Postconditions: lock is locked by the calling thread.

Remarks:If the function fails to meet the postcondition,terminate is called ([except.terminate]).

[ Note

:

This can happen if the re-locking of the mutex throws an exception.

end note

]

Throws:Any exception thrown by pred.

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);

Effects:Registers for the duration of this call *thisto get notified on a stop request on stokenduring this call and then equivalent to:

while (!stoken.stop_requested()) { if (pred()) return true; if (cv.wait_until(lock, abs_time) == cv_status::timeout) return pred(); } return pred();

[ Note

:

There is no blocking if pred() is initially true,stoken.stop_­requested() was already trueor the timeout has already expired.

end note

]

[ Note

:

The returned value indicates whether the predicate evaluated to trueregardless of whether the timeout was triggered or a stop request was made.

end note

]

Postconditions: lock is locked by the calling thread.

Remarks:If the function fails to meet the postcondition,terminate is called ([except.terminate]).

[ Note

:

This can happen if the re-locking of the mutex throws an exception.

end note

]

Throws:Timeout-related exceptions ([thread.req.timing]), or any exception thrown by pred.

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);

Effects:Equivalent to:

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

32.7 Semaphore [thread.sema]

Semaphores are lightweight synchronization primitives used to constrain concurrent access to a shared resource.

They are widely used to implement other synchronization primitives and, whenever both are applicable, can be more efficient than condition variables.

A counting semaphore is a semaphore object that models a non-negative resource count.

A binary semaphore is a semaphore object that has only two states.

A binary semaphore should be more efficient than the default implementation of a counting semaphore with a unit resource count.

32.7.1 Header synopsis [semaphore.syn]

namespace std { template class counting_semaphore;

using binary_semaphore = counting_semaphore<1>; }

32.7.2 Class template counting_­semaphore [thread.sema.cnt]

namespace std { template class counting_semaphore { public: static constexpr ptrdiff_t max() noexcept;

constexpr explicit counting_semaphore(ptrdiff_t desired);
~counting_semaphore();

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

void release(ptrdiff_t update = 1);
void acquire();
bool try_acquire() noexcept;
template<class Rep, class Period>
  bool try_acquire_for(const chrono::duration<Rep, Period>& rel_time);
template<class Clock, class Duration>
  bool try_acquire_until(const chrono::time_point<Clock, Duration>& abs_time);

private: ptrdiff_t counter;
}; }

Class template counting_­semaphore maintains an internal counter that is initialized when the semaphore is created.

The counter is decremented when a thread acquires the semaphore, and is incremented when a thread releases the semaphore.

If a thread tries to acquire the semaphore when the counter is zero, the thread will block until another thread increments the counter by releasing the semaphore.

least_­max_­value shall be non-negative; otherwise the program is ill-formed.

Concurrent invocations of the member functions of counting_­semaphore, other than its destructor, do not introduce data races.

static constexpr ptrdiff_t max() noexcept;

Returns:The maximum value of counter.

This value is greater than or equal to least_­max_­value.

constexpr explicit counting_semaphore(ptrdiff_t desired);

Preconditions: desired >= 0 is true, anddesired <= max() is true.

Effects:Initializes counter with desired.

void release(ptrdiff_t update = 1);

Preconditions: update >= 0 is true, andupdate <= max() - counter is true.

Effects:Atomically execute counter += update.

Then, unblocks any threads that are waiting for counter to be greater than zero.

Synchronization:Strongly happens before invocations of try_­acquirethat observe the result of the effects.

Throws: system_­error when an exception is required ([thread.req.exception]).

Error conditions:Any of the error conditions allowed for mutex types ([thread.mutex.requirements.mutex]).

bool try_acquire() noexcept;

Effects:Attempts to atomically decrement counter if it is positive, without blocking.

If counter is not decremented, there is no effect andtry_­acquire immediately returns.

An implementation may fail to decrement countereven if it is positive.

[ Note

:

This spurious failure is normally uncommon, but allows interesting implementations based on a simple compare and exchange ([atomics]).

end note

]

An implementation should ensure that try_­acquiredoes not consistently return falsein the absence of contending semaphore operations.

Returns: true if counter was decremented, otherwise false.

Effects:Repeatedly performs the following steps, in order:

Throws: system_­error when an exception is required ([thread.req.exception]).

Error conditions:Any of the error conditions allowed for mutex types ([thread.mutex.requirements.mutex]).

template<class Rep, class Period> bool try_acquire_for(const chrono::duration<Rep, Period>& rel_time);template<class Clock, class Duration> bool try_acquire_until(const chrono::time_point<Clock, Duration>& abs_time);

Effects:Repeatedly performs the following steps, in order:

The timeout expires ([thread.req.timing]) when the current time is after abs_­time (for try_­acquire_­until) or when at least rel_­time has passed from the start of the function (for try_­acquire_­for).

Throws:Timeout-related exceptions ([thread.req.timing]), or system_­errorwhen a non-timeout-related exception is required ([thread.req.exception]).

Error conditions:Any of the error conditions allowed for mutex types ([thread.mutex.requirements.mutex]).

32.8 Coordination types [thread.coord]

This subclause describes various concepts related to thread coordination, and defines the coordination types latch and barrier.

These types facilitate concurrent computation performed by a number of threads.

32.8.1 Latches [thread.latch]

A latch is a thread coordination mechanism that allows any number of threads to block until an expected number of threads arrive at the latch (via the count_­down function).

The expected count is set when the latch is created.

An individual latch is a single-use object; once the expected count has been reached, the latch cannot be reused.

32.8.1.1 Header synopsis [latch.syn]

namespace std { class latch; }

32.8.1.2 Class latch [thread.latch.class]

namespace std { class latch { public: static constexpr ptrdiff_t max() noexcept;

constexpr explicit latch(ptrdiff_t expected);
~latch();

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

void count_down(ptrdiff_t update = 1);
bool try_wait() const noexcept;
void wait() const;
void arrive_and_wait(ptrdiff_t update = 1);

private: ptrdiff_t counter;
}; }

A latch maintains an internal counter that is initialized when the latch is created.

Threads can block on the latch object, waiting for counter to be decremented to zero.

Concurrent invocations of the member functions of latch, other than its destructor, do not introduce data races.

static constexpr ptrdiff_t max() noexcept;

Returns:The maximum value of counter that the implementation supports.

constexpr explicit latch(ptrdiff_t expected);

Preconditions: expected >= 0 is true andexpected <= max() is true.

Effects:Initializes counter with expected.

void count_down(ptrdiff_t update = 1);

Preconditions: update >= 0 is true, andupdate <= counter is true.

Effects:Atomically decrements counter by update.

If counter is equal to zero, unblocks all threads blocked on *this.

Synchronization:Strongly happens before the returns from all calls that are unblocked.

Throws: system_­error when an exception is required ([thread.req.exception]).

Error conditions:Any of the error conditions allowed for mutex types ([thread.mutex.requirements.mutex]).

bool try_wait() const noexcept;

Returns:With very low probability false.

Otherwise counter == 0.

Effects:If counter equals zero, returns immediately.

Otherwise, blocks on *thisuntil a call to count_­down that decrements counter to zero.

Throws: system_­error when an exception is required ([thread.req.exception]).

Error conditions:Any of the error conditions allowed for mutex types ([thread.mutex.requirements.mutex]).

void arrive_and_wait(ptrdiff_t update = 1);

Effects:Equivalent to:

count_down(update); wait();

32.8.2 Barriers [thread.barrier]

A barrier is a thread coordination mechanism whose lifetime consists of a sequence of barrier phases, where each phase allows at most an expected number of threads to block until the expected number of threads arrive at the barrier.

[ Note

:

A barrier is useful for managing repeated tasks that are handled by multiple threads.

end note

]

32.8.2.1 Header synopsis [barrier.syn]

namespace std { template class barrier; }

32.8.2.2 Class template barrier [thread.barrier.class]

namespace std { template class barrier { public: using arrival_token = see below;

static constexpr ptrdiff_t max() noexcept;

constexpr explicit barrier(ptrdiff_t expected,
                           CompletionFunction f = CompletionFunction());
~barrier();

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

[[nodiscard]] arrival_token arrive(ptrdiff_t update = 1);
void wait(arrival_token&& arrival) const;

void arrive_and_wait();
void arrive_and_drop();

private: CompletionFunction completion;
}; }

Each barrier phase consists of the following steps:

Each phase defines a phase synchronization point.

Threads that arrive at the barrier during the phase can block on the phase synchronization point by calling wait, and will remain blocked until the phase completion step is run.

The phase completion stepthat is executed at the end of each phase has the following effects:

The end of the completion step strongly happens before the returns from all calls that were unblocked by the completion step.

For specializations that do not have the default value of the CompletionFunction template parameter, the behavior is undefined if any of the barrier object's member functions other than wait are called while the completion step is in progress.

Concurrent invocations of the member functions of barrier, other than its destructor, do not introduce data races.

The member functions arrive and arrive_­and_­dropexecute atomically.

CompletionFunction shall meet theCpp17MoveConstructible (Table 28) andCpp17Destructible (Table 32) requirements.

is_­nothrow_­invocable_­v<CompletionFunction&> shall be true.

The default value of the CompletionFunction template parameter is an unspecified type, such that, in addition to satisfying the requirements of CompletionFunction, it meets the Cpp17DefaultConstructiblerequirements (Table 27) andcompletion() has no effects.

barrier​::​arrival_­token is an unspecified type, such that it meets theCpp17MoveConstructible (Table 28),Cpp17MoveAssignable (Table 30), andCpp17Destructible (Table 32) requirements.

static constexpr ptrdiff_t max() noexcept;

Returns:The maximum expected count that the implementation supports.

constexpr explicit barrier(ptrdiff_t expected, CompletionFunction f = CompletionFunction());

Preconditions: expected >= 0 is true andexpected <= max() is true.

Effects:Sets both the initial expected count for each barrier phase and the current expected count for the first phase to expected.

Initializes completion with std​::​move(f).

Starts the first phase.

[ Note

:

If expected is 0 this object can only be destroyed.

end note

]

Throws:Any exception thrown by CompletionFunction's move constructor.

[[nodiscard]] arrival_token arrive(ptrdiff_t update = 1);

Preconditions: update > 0 is true, andupdate is less than or equal to the expected count for the current barrier phase.

Effects:Constructs an object of type arrival_­tokenthat is associated with the phase synchronization point for the current phase.

Then, decrements the expected count by update.

Synchronization:The call to arrive strongly happens before the start of the phase completion step for the current phase.

Returns:The constructed arrival_­token object.

Throws: system_­error when an exception is required ([thread.req.exception]).

Error conditions:Any of the error conditions allowed for mutex types ([thread.mutex.requirements.mutex]).

[ Note

:

This call can cause the completion step for the current phase to start.

end note

]

void wait(arrival_token&& arrival) const;

Preconditions: arrival is associated with the phase synchronization point for the current phase or the immediately preceding phase of the same barrier object.

Effects:Blocks at the synchronization point associated with std​::​move(arrival)until the phase completion step of the synchronization point's phase is run.

[ Note

:

If arrival is associated with the synchronization point for a previous phase, the call returns immediately.

end note

]

Throws: system_­error when an exception is required ([thread.req.exception]).

Error conditions:Any of the error conditions allowed for mutex types ([thread.mutex.requirements.mutex]).

Effects:Equivalent to: wait(arrive()).

Preconditions:The expected count for the current barrier phase is greater than zero.

Effects:Decrements the initial expected count for all subsequent phases by one.

Then decrements the expected count for the current phase by one.

Synchronization:The call to arrive_­and_­drop strongly happens before the start of the phase completion step for the current phase.

Throws: system_­error when an exception is required ([thread.req.exception]).

Error conditions:Any of the error conditions allowed for mutex types ([thread.mutex.requirements.mutex]).

[ Note

:

This call can cause the completion step for the current phase to start.

end note

]

32.9 Futures [futures]

32.9.1 Overview [futures.overview]

[futures] describes components that a C++ program can use to retrieve in one thread the result (value or exception) from a function that has run in the same thread or another thread.

[ Note

:

These components are not restricted to multi-threaded programs but can be useful in single-threaded programs as well.

end note

]

32.9.2 Header synopsis [future.syn]

namespace std { enum class future_errc { broken_promise = implementation-defined, future_already_retrieved = implementation-defined, promise_already_satisfied = implementation-defined, no_state = implementation-defined };

enum class launch : unspecified { async = unspecified, deferred = unspecified, implementation-defined };

enum class future_status { ready, timeout, deferred };

template<> struct is_error_code_enum : public true_type { }; error_code make_error_code(future_errc e) noexcept; error_condition make_error_condition(future_errc e) noexcept;

const error_category& future_category() noexcept;

class future_error;

template class promise; template class promise<R&>; template<> class promise;

template void swap(promise& x, promise& y) noexcept;

template<class R, class Alloc> struct uses_allocator<promise, Alloc>;

template class future; template class future<R&>; template<> class future;

template class shared_future; template class shared_future<R&>; template<> class shared_future;

template class packaged_task;
template<class R, class... ArgTypes> class packaged_task<R(ArgTypes...)>;

template<class R, class... ArgTypes> void swap(packaged_task<R(ArgTypes...)>&, packaged_task<R(ArgTypes...)>&) noexcept;

template<class F, class... Args> [[nodiscard]] future<invoke_result_t<decay_t, decay_t...>> async(F&& f, Args&&... args); template<class F, class... Args> [[nodiscard]] future<invoke_result_t<decay_t, decay_t...>> async(launch policy, F&& f, Args&&... args); }

The enum type launch is a bitmask type with elements launch​::​async and launch​::​deferred.

[ Note

:

Implementations can provide bitmasks to specify restrictions on task interaction by functions launched by async() applicable to a corresponding subset of available launch policies.

Implementations can extend the behavior of the first overload of async() by adding their extensions to the launch policy under the “as if” rule.

end note

]

The enum values of future_­errc are distinct and not zero.

32.9.3 Error handling [futures.errors]

const error_category& future_category() noexcept;

Returns:A reference to an object of a type derived from class error_­category.

The object's default_­error_­condition and equivalent virtual functions shall behave as specified for the class error_­category.

The object's namevirtual function returns a pointer to the string "future".

error_code make_error_code(future_errc e) noexcept;

Returns: error_­code(static_­cast<int>(e), future_­category()).

error_condition make_error_condition(future_errc e) noexcept;

Returns: error_­condition(static_­cast<int>(e), future_­category()).

32.9.4 Class future_­error [futures.future.error]

namespace std { class future_error : public logic_error { public: explicit future_error(future_errc e);

const error_code& code() const noexcept;
const char*       what() const noexcept;

private: error_code ec_;
}; }

explicit future_error(future_errc e);

Effects:Initializes ec_­ with make_­error_­code(e).

const error_code& code() const noexcept;

const char* what() const noexcept;

Returns:An ntbs incorporating code().message().

32.9.5 Shared state [futures.state]

Many of the classes introduced in subclause [futures] use some state to communicate results.

Thisshared state consists of some state information and some (possibly not yet evaluated) result, which can be a (possibly void) value or an exception.

[ Note

:

Futures, promises, and tasks defined in this clause reference such shared state.

end note

]

[ Note

:

The result can be any kind of object including a function to compute that result, as used by async when policy is launch​::​deferred.

end note

]

An asynchronous return object is an object that reads results from a shared state.

A waiting function of an asynchronous return object is one that potentially blocks to wait for the shared state to be made ready.

If a waiting function can return before the state is made ready because of a timeout ([thread.req.lockable]), then it is a timed waiting function, otherwise it is a non-timed waiting function.

An asynchronous provider is an object that provides a result to a shared state.

The result of a shared state is set by respective functions on the asynchronous provider.

[ Note

:

Such as promises or tasks.

end note

]

The means of setting the result of a shared state is specified in the description of those classes and functions that create such a state object.

When an asynchronous return object or an asynchronous provider is said to release its shared state, it means:

When an asynchronous provider is said to make its shared state ready, it means:

When an asynchronous provider is said to abandon its shared state, it means:

A shared state is ready only if it holds a value or an exception ready for retrieval.

Waiting for a shared state to become ready may invoke code to compute the result on the waiting thread if so specified in the description of the class or function that creates the state object.

Calls to functions that successfully set the stored result of a shared state synchronize with calls to functions successfully detecting the ready state resulting from that setting.

The storage of the result (whether normal or exceptional) into the shared statesynchronizes withthe successful return from a call to a waiting function on the shared state.

Some functions (e.g., promise​::​set_­value_­at_­thread_­exit) delay making the shared state ready until the calling thread exits.

The destruction of each of that thread's objects with thread storage durationis sequenced before making that shared state ready.

Access to the result of the same shared state may conflict.

[ Note

:

This explicitly specifies that the result of the shared state is visible in the objects that reference this state in the sense of data race avoidance ([res.on.data.races]).

For example, concurrent accesses through references returned by shared_­future​::​get() ([futures.shared.future]) must either use read-only operations or provide additional synchronization.

end note

]

32.9.6 Class template promise [futures.promise]

namespace std { template class promise { public: promise(); template promise(allocator_arg_t, const Allocator& a); promise(promise&& rhs) noexcept; promise(const promise&) = delete; ~promise();

promise& operator=(promise&& rhs) noexcept;
promise& operator=(const promise&) = delete;
void swap(promise& other) noexcept;


future<R> get_future();


void set_value(see below);
void set_exception(exception_ptr p);


void set_value_at_thread_exit(see below);
void set_exception_at_thread_exit(exception_ptr p);

};

template void swap(promise& x, promise& y) noexcept;

template<class R, class Alloc> struct uses_allocator<promise, Alloc>; }

The implementation provides the template promise and two specializations,promise<R&> and promise<​void>.

These differ only in the argument type of the member functions set_­value and set_­value_­at_­thread_­exit, as set out in their descriptions, below.

The set_­value, set_­exception, set_­value_­at_­thread_­exit, and set_­exception_­at_­thread_­exit member functions behave as though they acquire a single mutex associated with the promise object while updating the promise object.

template<class R, class Alloc> struct uses_allocator<promise<R>, Alloc> : true_type { };

Preconditions: Alloc meets the Cpp17Allocatorrequirements (Table 36).

promise();template<class Allocator> promise(allocator_arg_t, const Allocator& a);

Effects:Creates a shared state.

The second constructor uses the allocator a to allocate memory for the shared state.

promise(promise&& rhs) noexcept;

Effects:Transfers ownership of the shared state of rhs (if any) to the newly-constructed object.

Postconditions: rhs has no shared state.

Effects:Abandons any shared state ([futures.state]).

promise& operator=(promise&& rhs) noexcept;

Effects:Abandons any shared state ([futures.state]) and then as ifpromise(std​::​move(rhs)).swap(*this).

void swap(promise& other) noexcept;

Effects:Exchanges the shared state of *this and other.

Postconditions: *this has the shared state (if any) that other had prior to the call to swap.

other has the shared state (if any) that*this had prior to the call to swap.

Returns:A future<R> object with the same shared state as*this.

Synchronization:Calls to this function do not introduce data races ([intro.multithread]) with calls toset_­value,set_­exception,set_­value_­at_­thread_­exit, orset_­exception_­at_­thread_­exit.

[ Note

:

Such calls need not synchronize with each other.

end note

]

Throws: future_­error if *this has no shared state or ifget_­future has already been called on a promise with the same shared state as *this.

Error conditions:

void promise::set_value(const R& r);void promise::set_value(R&& r);void promise<R&>::set_value(R& r);void promise<void>::set_value();

Effects:Atomically stores the value r in the shared state and makes that state ready ([futures.state]).

Throws:

Error conditions:

void set_exception(exception_ptr p);

Preconditions: p is not null.

Effects:Atomically stores the exception pointer p in the shared state and makes that state ready ([futures.state]).

Throws: future_­error if its shared state already has a stored value or exception.

Error conditions:

void promise::set_value_at_thread_exit(const R& r);void promise::set_value_at_thread_exit(R&& r);void promise<R&>::set_value_at_thread_exit(R& r);void promise<void>::set_value_at_thread_exit();

Effects:Stores the value r in the shared state without making that state ready immediately.

Schedules that state to be made ready when the current thread exits, after all objects of thread storage duration associated with the current thread have been destroyed.

Throws:

Error conditions:

void set_exception_at_thread_exit(exception_ptr p);

Preconditions: p is not null.

Effects:Stores the exception pointer p in the shared state without making that state ready immediately.

Schedules that state to be made ready when the current thread exits, after all objects of thread storage duration associated with the current thread have been destroyed.

Throws: future_­error if an error condition occurs.

Error conditions:

template<class R> void swap(promise<R>& x, promise<R>& y) noexcept;

Effects:As if by x.swap(y).

32.9.7 Class template future [futures.unique.future]

The class template future defines a type for asynchronous return objects which do not share their shared state with other asynchronous return objects.

A default-constructed future object has no shared state.

A future object with shared state can be created by functions on asynchronous providersor by the move constructor and shares its shared state with the original asynchronous provider.

The result (value or exception) of a future object can be set by calling a respective function on an object that shares the same shared state.

[ Note

:

Member functions of future do not synchronize with themselves or with member functions of shared_­future.

end note

]

The effect of calling any member function other than the destructor, the move-assignment operator, share, or valid on a future object for whichvalid() == falseis undefined.

[ Note

:

It is valid to move from a future object for which valid() == false.

end note

]

[ Note

:

Implementations should detect this case and throw an object of typefuture_­error with an error condition of future_­errc​::​no_­state.

end note

]

namespace std { template class future { public: future() noexcept; future(future&&) noexcept; future(const future&) = delete; ~future(); future& operator=(const future&) = delete; future& operator=(future&&) noexcept; shared_future share() noexcept;

see below get();


bool valid() const noexcept;

void wait() const;
template<class Rep, class Period>
  future_status wait_for(const chrono::duration<Rep, Period>& rel_time) const;
template<class Clock, class Duration>
  future_status wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;

}; }

The implementation provides the template future and two specializations,future<R&> and future<​void>.

These differ only in the return type and return value of the member function get, as set out in its description, below.

Effects:The object does not refer to a shared state.

Postconditions: valid() == false.

future(future&& rhs) noexcept;

Effects:Move constructs a future object that refers to the shared state that was originally referred to by rhs (if any).

Postconditions:

Effects:

future& operator=(future&& rhs) noexcept;

Effects:

Postconditions:

shared_future<R> share() noexcept;

Returns: shared_­future<R>(std​::​move(*this)).

Postconditions: valid() == false.

R future::get(); R& future<R&>::get();void future<void>::get();

[ Note

:

As described above, the template and its two required specializations differ only in the return type and return value of the member function get.

end note

]

Effects:

Returns:

Throws:The stored exception, if an exception was stored in the shared state.

Postconditions: valid() == false.

bool valid() const noexcept;

Returns: true only if *this refers to a shared state.

Effects:Blocks until the shared state is ready.

template<class Rep, class Period> future_status wait_for(const chrono::duration<Rep, Period>& rel_time) const;

Effects:None if the shared state contains a deferred function ([futures.async]), otherwise blocks until the shared state is ready or until the relative timeout ([thread.req.timing]) specified by rel_­time has expired.

Returns:

Throws:timeout-related exceptions ([thread.req.timing]).

template<class Clock, class Duration> future_status wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;

Effects:None if the shared state contains a deferred function ([futures.async]), otherwise blocks until the shared state is ready or until the absolute timeout ([thread.req.timing]) specified by abs_­time has expired.

Returns:

Throws:timeout-related exceptions ([thread.req.timing]).

32.9.9 Function template async [futures.async]

The function template async provides a mechanism to launch a function potentially in a new thread and provides the result of the function in a future object with which it shares a shared state.

template<class F, class... Args> [[nodiscard]] future<invoke_result_t<decay_t<F>, decay_t<Args>...>> async(F&& f, Args&&... args);template<class F, class... Args> [[nodiscard]] future<invoke_result_t<decay_t<F>, decay_t<Args>...>> async(launch policy, F&& f, Args&&... args);

Mandates:The following are all true:

Preconditions: decay_­t<F> and each type in decay_­t<Args> meet the Cpp17MoveConstructible requirements.

Effects:The first function behaves the same as a call to the second function with a policy argument oflaunch​::​async | launch​::​deferredand the same arguments for F and Args.

The second function creates a shared state that is associated with the returned future object.

The further behavior of the second function depends on the policy argument as follows (if more than one of these conditions applies, the implementation may choose any of the corresponding policies):

Returns:An object of typefuture<invoke_­result_­t<decay_­t<F>, decay_­t<Args>...>> that refers to the shared state created by this call to async.

[ Note

:

If a future obtained from async is moved outside the local scope, other code that uses the future should be aware that the future's destructor can block for the shared state to become ready.

end note

]

Synchronization:Regardless of the provided policy argument,

If the implementation chooses the launch​::​async policy,

Throws: system_­error if policy == launch​::​async and the implementation is unable to start a new thread, orstd​::​bad_­alloc if memory for the internal data structures could not be allocated.

Error conditions:

[ Example

:

int work1(int value); int work2(int value); int work(int value) { auto handle = std::async([=]{ return work2(value); }); int tmp = work1(value); return tmp + handle.get();
}

[ Note

:

Line #1 might not result in concurrency because the async call uses the default policy, which may uselaunch​::​deferred, in which case the lambda might not be invoked until theget() call; in that case, work1 and work2 are called on the same thread and there is no concurrency.

end note

]

end example

]

32.9.10 Class template packaged_­task [futures.task]

The class template packaged_­task defines a type for wrapping a function or callable object so that the return value of the function or callable object is stored in a future when it is invoked.

When the packaged_­task object is invoked, its stored task is invoked and the result (whether normal or exceptional) stored in the shared state.

Any futures that share the shared state will then be able to access the stored result.

namespace std { template class packaged_task;

template<class R, class... ArgTypes> class packaged_task<R(ArgTypes...)> { public:

packaged_task() noexcept;
template<class F>
  explicit packaged_task(F&& f);
~packaged_task();


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


packaged_task(packaged_task&& rhs) noexcept;
packaged_task& operator=(packaged_task&& rhs) noexcept;
void swap(packaged_task& other) noexcept;

bool valid() const noexcept;


future<R> get_future();


void operator()(ArgTypes... );
void make_ready_at_thread_exit(ArgTypes...);

void reset();

};

template<class R, class... ArgTypes> void swap(packaged_task<R(ArgTypes...)>& x, packaged_task<R(ArgTypes...)>& y) noexcept; }

32.9.10.1 Member functions [futures.task.members]

packaged_task() noexcept;

Effects:The object has no shared state and no stored task.

template<class F> packaged_task(F&& f);

Constraints: remove_­cvref_­t<F>is not the same type as packaged_­task<R(ArgTypes...)>.

Mandates: is_­invocable_­r_­v<R, F&, ArgTypes...> is true.

Preconditions:Invoking a copy of f behaves the same as invoking f.

Effects:Constructs a new packaged_­task object with a shared state and initializes the object's stored task with std​::​forward<F>(f).

Throws:Any exceptions thrown by the copy or move constructor of f, orbad_­alloc if memory for the internal data structures could not be allocated.

packaged_task(packaged_task&& rhs) noexcept;

Effects:Transfers ownership ofrhs's shared state to *this, leaving rhs with no shared state.

Moves the stored task from rhs to *this.

Postconditions: rhs has no shared state.

packaged_task& operator=(packaged_task&& rhs) noexcept;

Effects:

Effects:Abandons any shared state ([futures.state]).

void swap(packaged_task& other) noexcept;

Effects:Exchanges the shared states and stored tasks of *this and other.

Postconditions: *this has the same shared state and stored task (if any) as otherprior to the call to swap.

other has the same shared state and stored task (if any) as *this prior to the call to swap.

bool valid() const noexcept;

Returns: true only if *this has a shared state.

Returns:A future object that shares the same shared state as *this.

Synchronization:Calls to this function do not introduce data races ([intro.multithread]) with calls tooperator() ormake_­ready_­at_­thread_­exit.

[ Note

:

Such calls need not synchronize with each other.

end note

]

Throws:A future_­error object if an error occurs.

Error conditions:

void operator()(ArgTypes... args);

Effects:As if by INVOKE<R>(f, t, t, …, t) ([func.require]), where f is the stored task of *this andt, t, …, t are the values in args....

If the task returns normally, the return value is stored as the asynchronous result in the shared state of*this, otherwise the exception thrown by the task is stored.

The shared state of *this is made ready, and any threads blocked in a function waiting for the shared state of *this to become ready are unblocked.

Throws:A future_­error exception object if there is no shared state or the stored task has already been invoked.

Error conditions:

void make_ready_at_thread_exit(ArgTypes... args);

Effects:As if by INVOKE<R>(f, t, t, …, t) ([func.require]), where f is the stored task andt, t, …, t are the values in args....

If the task returns normally, the return value is stored as the asynchronous result in the shared state of*this, otherwise the exception thrown by the task is stored.

In either case, this is done without making that state ready ([futures.state]) immediately.

Schedules the shared state to be made ready when the current thread exits, after all objects of thread storage duration associated with the current thread have been destroyed.

Throws: future_­error if an error condition occurs.

Error conditions:

Effects:As if *this = packaged_­task(std​::​move(f)), wheref is the task stored in*this.

[ Note

:

This constructs a new shared state for *this.

The old state is abandoned ([futures.state]).

end note

]

Throws:

32.9.10.2 Globals [futures.task.nonmembers]

template<class R, class... ArgTypes> void swap(packaged_task<R(ArgTypes...)>& x, packaged_task<R(ArgTypes...)>& y) noexcept;

Effects:As if by x.swap(y).