std::shared_future<T>:: wait
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Member functions | ||||
| Getting the result | ||||
| State | ||||
|
shared_future::wait
|
||||
|
void
wait
(
)
const
;
|
(自 C++11 起) | |
阻塞直到结果可用。调用后 valid ( ) == true 。
若在调用此函数前
valid
(
)
==
false
,则行为未定义。
目录 |
参数
(无)
返回值
(无)
异常
可能抛出实现定义的异常。
注释
鼓励实现在调用前检测 valid ( ) == false 的情况,并抛出带有错误条件 std::future_error 的 std::future_errc::no_state 异常。
在多个线程中对同一个
std::shared_future
调用wait操作是不安全的;正确的使用方式是让每个等待同一共享状态的线程都持有一个
std::shared_future
的副本。
示例
#include <chrono> #include <future> #include <iostream> #include <thread> int fib(int n) { if (n < 3) return 1; else return fib(n - 1) + fib(n - 2); } int main() { std::shared_future<int> f1 = std::async(std::launch::async, []() { return fib(40); }); std::shared_future<int> f2 = std::async(std::launch::async, []() { return fib(43); }); std::cout << "waiting... " << std::flush; const auto start = std::chrono::system_clock::now(); f1.wait(); f2.wait(); const auto diff = std::chrono::system_clock::now() - start; std::cout << std::chrono::duration<double>(diff).count() << " seconds\n"; std::cout << "f1: " << f1.get() << '\n'; std::cout << "f2: " << f2.get() << '\n'; }
可能的输出:
waiting... 1.61803 seconds f1: 102334155 f2: 433494437
参见
|
等待结果,若在指定的超时时间内结果不可用则返回
(公开成员函数) |
|
|
等待结果,若在到达指定时间点前结果不可用则返回
(公开成员函数) |