Template Class Future

Nested Relationships

Nested Types

Class Documentation

template<typename T>
class Future

The result of one asynchronous operation, completed by a worker thread.

The completion is recorded in C++ only (a mutex and a condition variable), so the worker thread never needs the Python GIL; the threads that wait for the result convert it to Python. Copies of a future share its state.

The result is destroyed with the last copy of the future. A worker that holds a copy must release it on a thread that may run the result’s destructor (for example, one that may call a Python allocator’s deleter).

T must be copyable: get returns a copy, so the result can be read any number of times, as with concurrent.futures.Future.result().

Public Functions

inline explicit Future(std::optional<CompletionQueue> queue = std::nullopt)

Creates a pending future. If queue is given, the future’s ID is pushed to it on completion.

inline uint64_t id() const

Identifier unique within the process.

inline void set_result(T result)

Completes the future with a result. Later completions are ignored.

inline void set_error(std::exception_ptr error)

Completes the future with an error. Later completions are ignored. A null error is replaced with a std::runtime_error, so a completed future always holds either a result or an error.

inline bool done() const

Whether the future is completed.

inline bool wait(std::optional<double> timeout = std::nullopt) const

Waits until the future is completed, or until timeout seconds pass. nullopt (or a timeout too long to represent) waits indefinitely.

Returns:

Whether the future is completed.

inline T get() const

Waits for the future and returns a copy of its result, or rethrows its error.

inline std::exception_ptr exception() const

Waits for the future and returns its error, or nullptr if it succeeded.