首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::future::wait

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

二次

代码语言:javascript
复制
#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';
}

二次

产出:

二次

代码语言:javascript
复制
waiting...
f1: 6765
f2: 75025

二次

另见

wait_for

waits for the result, returns if it is not available for the specified timeout duration (public member function)

wait_until

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

代码语言:txt
复制
 ? cppreference.com

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

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com