std::future::wait

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

阻塞直至结果变得可用。调用后 valid() == true

若调用此函数前 valid()== false 则行为未定义。

目录

参数

(无)

返回值

(无)

异常

(无)

注意

鼓励实现检测调用前 valid == false 的情况并抛出以 std::future_errc::no_state 为 error_condition 的 std::future_error


示例

#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

参阅

等待结果,如果在指定的超时间隔后仍然无法得到结果,则返回。
(公开成员函数)
等待结果,如果在已经到达指定的时间点时仍然无法得到结果,则返回。
(公开成员函数)

版本历史

  • (当前 | 先前 2017年9月24日 (日) 07:38Fruderica讨论 | 贡献 . . (85字节) (-202). . (撤销)
  • 当前 | 先前 2013年7月2日 (二) 11:31P12bot讨论 | 贡献 . . (287字节) (-1). . (Use {{lc}}. Update links. Various fixes.) (撤销)
  • 当前 | 先前 2012年11月2日 (五) 20:00P12bot讨论 | 贡献 . . (288字节) (+241). . (r2.7.3) (机器人添加:de, en, es, fr, it, ja, pt, ru) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 03:08P12讨论 | 贡献 . . (47字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前) 2012年10月22日 (一) 14:48TranslationBot讨论 | 贡献. . (47字节) (+47). . (Translated from the English version using Google Translate)