description | title | ms.date | f1_keywords | ms.assetid | helpviewer_keywords | ms.custom | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Learn more about: promise Class |
promise Class |
06/17/2022 |
|
2931558c-d94a-4ba1-ac4f-20bf7b6e23f9 |
|
devdivchpfy22 |
Describes an asynchronous provider.
template <class Ty>
class promise;
Name | Description |
---|---|
promise | Constructs a promise object. |
Name | Description |
---|---|
get_future | Returns a future associated with this promise. |
set_exception | Atomically sets the result of this promise to indicate an exception. |
set_exception_at_thread_exit | Atomically sets the result of this promise to indicate an exception, and delivers the notification only after all thread-local objects in the current thread have been destroyed (usually at thread exit). |
set_value | Atomically sets the result of this promise to indicate a value. |
set_value_at_thread_exit | Atomically sets the result of this promise to indicate a value, and delivers the notification only after all thread-local objects in the current thread have been destroyed (usually at thread exit). |
swap | Exchanges the associated asynchronous state of this promise with that of a specified promise object. |
Name | Description |
---|---|
promise::operator= | Assignment of the shared state of this promise object. |
promise
Header: <future>
Namespace: std
Returns a future object that has the same associated asynchronous state as this promise.
future<Ty> get_future();
If the promise object is empty, this method throws a future_error that has an error_code of no_state
.
If this method has already been called for a promise object that has the same associated asynchronous state, the method throws a future_error
that has an error_code
of future_already_retrieved
.
Transfers the associated asynchronous state from a specified promise
object.
promise& operator=(promise&& Other) noexcept;
Other
A promise
object.
*this
This operator transfers the associated asynchronous state from Other. After the transfer, Other is empty.
Constructs a promise
object.
promise();
template <class Alloc>
promise(allocator_arg_t, const Alloc& Al);
promise(promise&& Other) noexcept;
Al
A memory allocator. For more information about allocators, see <allocators>.
Other
A promise
object.
The first constructor constructs an empty promise
object.
The second constructor constructs an empty promise
object and uses Al for memory allocation.
The third constructor constructs a promise
object and transfers the associated asynchronous state from Other, and leaves Other empty.
Atomically stores an exception as the result of this promise
object and sets the associated asynchronous state to ready.
void set_exception(exception_ptr Exc);
Exc
An exception_ptr that's stored by this method as the exception result.
If the promise
object has no associated asynchronous state, this method throws a future_error that has an error code of no_state
.
If set_exception
, set_exception_at_thread_exit, set_value, or set_value_at_thread_exit has already been called for a promise
object that has the same associated asynchronous state, this method throws a future_error
that has an error code of promise_already_satisfied
.
As a result of this method, any threads that are blocked on the associated asynchronous state become unblocked.
Atomically sets the result of this promise
to indicate an exception, delivering the notification only after all thread-local objects in the current thread have been destroyed (usually at thread exit).
void set_exception_at_thread_exit(exception_ptr Exc);
Exc
An exception_ptr that's stored by this method as the exception result.
If the promise object has no associated asynchronous state, this method throws a future_error that has an error code of no_state
.
If set_exception, set_exception_at_thread_exit
, set_value, or set_value_at_thread_exit has already been called for a promise
object that has the same associated asynchronous state, this method throws a future_error
that has an error code of promise_already_satisfied
.
In contrast to set_exception, this method doesn't set the associated asynchronous state to ready until after all thread-local objects in the current thread have been destroyed. Typically, threads that are blocked on the associated asynchronous state aren't unblocked until the current thread exits.
Atomically stores a value as the result of this promise
object and sets the associated asynchronous state to ready.
void promise::set_value(const Ty& Val);
void promise::set_value(Ty&& Val);
void promise<Ty&>::set_value(Ty& Val);
void promise<void>::set_value();
Val
The value to be stored as the result.
If the promise
object has no associated asynchronous state, this method throws a future_error that has an error code of no_state
.
If set_exception, set_exception_at_thread_exit, set_value
, or set_value_at_thread_exit has already been called for a promise
object that has the same associated asynchronous state, this method throws a future_error
that has an error code of promise_already_satisfied
.
As a result of this method, any threads that are blocked on the associated asynchronous state become unblocked.
The first method also throws any exception that is thrown when Val is copied into the associated asynchronous state. In this situation, the associated asynchronous state isn't set to ready.
The second method also throws any exception that is thrown when Val is moved into the associated asynchronous state. In this situation, the associated asynchronous state isn't set to ready.
For the partial specialization promise<Ty&>
, the stored value is in effect a reference to Val.
For the specialization promise<void>
, no stored value exists.
Atomically stores a value as the result of this promise
object.
void promise::set_value_at_thread_exit(const Ty& Val);
void promise::set_value_at_thread_exit(Ty&& Val);
void promise<Ty&>::set_value_at_thread_exit(Ty& Val);
void promise<void>::set_value_at_thread_exit();
Val
The value to be stored as the result.
If the promise object has no associated asynchronous state, this method throws a future_error that has an error code of no_state
.
If set_exception, set_exception_at_thread_exit, set_value, or set_value_at_thread_exit
has already been called for a promise
object that has the same associated asynchronous state, this method throws a future_error
that has an error code of promise_already_satisfied
.
In contrast to set_value
, the associated asynchronous state isn't set to ready until after all thread-local objects in the current thread have been destroyed. Typically, threads that are blocked on the associated asynchronous state aren't unblocked until the current thread exits.
The first method also throws any exception that is thrown when Val is copied into the associated asynchronous state.
The second method also throws any exception that is thrown when Val is moved into the associated asynchronous state.
For the partial specialization promise<Ty&>
, the stored value is effectively a reference to Val.
For the specialization promise<void>
, no stored value exists.
Exchanges the associated asynchronous state of this promise object with that of a specified object.
void swap(promise& Other) noexcept;
Other
A promise
object.