future Class

 

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

The latest version of this topic can be found at future Class.

Describes an asynchronous return object.

template <class Ty>
class future;

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.

Public Constructors

NameDescription
future::future ConstructorConstructs a future object.

Public Methods

NameDescription
future::get MethodRetrieves the result that is stored in the associated asynchronous state.
future::share MethodConverts the object to a shared_future.
future::valid MethodSpecifies whether the object is not empty.
future::wait MethodBlocks the current thread until the associated asynchronous state is ready.
future::wait_for MethodBlocks until the associated asynchronous state is ready or until the specified time has elapsed.
future::wait_until MethodBlocks until the associated asynchronous state is ready or until a specified point in time.

Public Operators

NameDescription
future::operator=Transfers the associated asynchronous state from a specified object.

Header: future

Namespace: std

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.

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.

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.

Converts the object to a shared_future object.

shared_future<Ty> share();

Return Value

shared_future(move(*this))

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.

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.

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.

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.

Header Files Reference
<future>

Show: