Namespaces
Variants

std::promise<R>:: get_future

From cppreference.net
Concurrency support library
Threads
(C++11)
(C++20)
this_thread namespace
(C++11)
(C++11)
Cooperative cancellation
Mutual exclusion
Generic lock management
Condition variables
(C++11)
Semaphores
Latches and Barriers
(C++20)
(C++20)
Futures
(C++11)
(C++11)
(C++11)
Safe reclamation
Hazard pointers
Atomic types
(C++11)
(C++20)
Initialization of atomic types
(C++11) (deprecated in C++20)
(C++11) (deprecated in C++20)
Memory ordering
(C++11) (deprecated in C++26)
Free functions for atomic operations
Free functions for atomic flags
std:: future < R > get_future ( ) ;
(自 C++11 起)

返回一个与 * this 共享相同共享状态的 future 对象。

* this 未包含共享状态或已调用过 get_future 方法,则会抛出异常。如需在 promise-future 通信通道中获取多个"消费"端,请使用 std::future::share

调用此函数不会与对 set_value set_exception set_value_at_thread_exit set_exception_at_thread_exit 的调用产生数据竞争(因此它们不需要彼此同步)。

参数

(无)

返回值

一个指向 * this 共享状态的 future。

异常

std::future_error 在以下情况下发生:

  • * this 没有共享状态。错误码被设置为 no_state
  • 已在与 * this 具有相同共享状态的 promise 上调用过 get_future() 。错误码被设置为 future_already_retrieved