在线文档教程
C++
线程支持 | Thread support

std::shared_future::wait

STD::共享[医]未来::等待

void wait() const;(since C++11)

块,直到结果可用为止。valid() == true打完电话后。

如果valid()== false在调用此函数之前。

参数

%280%29

返回值

%280%29

例外

%280%29

注记

鼓励实现在下列情况下检测情况:valid == false在呼叫前抛出一个std::future_error错误条件为std::future_errc::no_state...

呼叫等待相同std::shared_future来自多个线程是不安全的;预期的用途是每个等待相同共享状态的线程都有一个std::shared_future...

二次

#include <iostream> #include <future> #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(20 } std::shared_future<int> f2 = std::async(std::launch::async, [](){ return fib(25 } std::cout << "waiting...\n"; f1.wait( f2.wait( std::cout << "f1: " << f1.get() << '\n'; std::cout << "f2: " << f2.get() << '\n'; }

二次

产出:

二次

waiting... f1: 6765 f2: 75025

二次

另见

wait_forwaits for the result, returns if it is not available for the specified timeout duration (public member function)
wait_untilwaits for the result, returns if it is not available until specified time point has been reached (public member function)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/线程/Shared[医]未来/等待