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

std::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...

二次

#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::future<int> f1 = std::async(std::launch::async, [](){ return fib(20 } std::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/线程/前途/等待