future Class
The latest version of this topic can be found at future Class.
Describes an asynchronous return object.
Syntax
template <class Ty>
class future;
Remarks
Each standard asynchronous provider returns an object whose type is an instantiation of this template. A future
object provides the only access to the asynchronous provider that it is associated with. If you need multiple asynchronous return objects that are associated with the same asynchronous provider, copy the future
object to a shared_future object.
Members
Public Constructors
Name | Description |
---|---|
future::future Constructor | Constructs a future object. |
Public Methods
Name | Description |
---|---|
future::get Method | Retrieves the result that is stored in the associated asynchronous state. |
future::share Method | Converts the object to a shared_future . |
future::valid Method | Specifies whether the object is not empty. |
future::wait Method | Blocks the current thread until the associated asynchronous state is ready. |
future::wait_for Method | Blocks until the associated asynchronous state is ready or until the specified time has elapsed. |
future::wait_until Method | Blocks until the associated asynchronous state is ready or until a specified point in time. |
Public Operators
Name | Description |
---|---|
future::operator= | Transfers the associated asynchronous state from a specified object. |
Requirements
Header: future
Namespace: std
future::future Constructor
Constructs a future
object.
future() noexcept;
future(future&& Other) noexcept;
Parameters
Other
A future
object.
Remarks
The first constructor constructs a future
object that has no associated asynchronous state.
The second constructor constructs a future
object and transfers the associated asynchronous state from Other
. Other
no longer has an associated asynchronous state.
future::get Method
Retrieves the result that is stored in the associated asynchronous state.
Ty get();
Return Value
If the result is an exception, the method rethrows it. Otherwise, the result is returned.
Remarks
Before it retrieves the result, this method blocks the current thread until the associated asynchronous state is ready.
For the partial specialization future<Ty&>
, the stored value is effectively a reference to the object that was passed to the asynchronous provider as the return value.
Because no stored value exists for the specialization future<void>
, the method returns void
.
In other specializations, the method moves its return value from the stored value. Therefore, call this method only once.
future::operator=
Transfers an associated asynchronous state from a specified object.
future& operator=(future&& Right) noexcept;
Parameters
Right
A future
object.
Return Value
*this
Remarks
After the transfer, Right
no longer has an associated asynchronous state.
future::share Method
Converts the object to a shared_future object.
shared_future<Ty> share();
Return Value
shared_future(move(*this))
future::valid Method
Specifies whether the object has an associated asynchronous state.
bool valid() noexcept;
Return Value
true
if the object has an associated asynchronous state; otherwise, false
.
future::wait Method
Blocks the current thread until the associated asynchronous state is ready.
void wait() const;
Remarks
An associated asynchronous state is ready only if its asynchronous provider has stored a return value or stored an exception.
future::wait_for Method
Blocks the current thread until the associated asynchronous state is ready or until a specified time interval has elapsed.
template <class Rep, class Period>
future_status wait_for(
const chrono::duration<Rep, Period>& Rel_time) const;
Parameters
Rel_time
A chrono::duration object that specifies a maximum time interval that the thread blocks.
Return Value
A future_status that indicates the reason for returning.
Remarks
An associated asynchronous state is ready only if its asynchronous provider has stored a return value or stored an exception.
future::wait_until Method
Blocks the current thread until the associated asynchronous state is ready or until after a specified time point.
template <class Clock, class Duration>
future_status wait_until(
const chrono::time_point<Clock, Duration>& Abs_time) const;
Parameters
Abs_time
A chrono::time_point object that specifies a time after which the thread can unblock.
Return Value
A future_status that indicates the reason for returning.
Remarks
An associated asynchronous state is ready only if its asynchronous provider has stored a return value or stored an exception.