C++11 future

一 前面
  • C++11开始,对并发提供了强有力的支持,本文介绍其中的 future 和 shared_future。
二 future
  • 类模板 std::future 提供访问异步操作结果的机制。
  • 通常与 std::async、std::packaged_task、std::promise等搭配使用。
三 成员函数
  1. 构造函数

    future() noexcept; (1)(C++11)
    future( future&& other ) noexcept; (2)(C++11)
    future( const future& other ) = delete; (3)(C++11)
    
  • 即 future 不可复制构造,这也体现在 operator= 函数中。
  1. operator=

    future& operator=( future&& other ) noexcept; (1)(C++11)
    future& operator=( const future& other ) = delete; (2)(C++11)
    
  2. share

    std::shared_future<T> share() noexcept;
    
  • 转移共享状态到一个 std::shared_future对象中。若valid() = false,返回空。
  • 调用后 valid() 为 false。
  1. get
  • 等待异步操作结束并获取结果。
  • 调用后 valid() 为 false。
  • 若 valid() = false , 行为未定义。即仅可调用一次
  1. valid
  • std::future 对象是否有效,即是否与某个共享状态相关联。
  • 非默认构造或从 std::promise::get_future()、std::packaged_task::get_future() 、std::async()得到的 future对象在第一次调用 get() 或 shared()前是有效的。
  1. wait
  • 等待直至结果可用。
  1. wait_for

    template< class Rep, class Period >
    std::future_status wait_for( 
    const std::chrono::duration<Rep,Period>& timeout_duration ) const;(C++11)
    
  • 等待指定时间间隔,结果可用或超时均返回。

  • 时间间隔,请参见此前文章 C++11 std::duration

  • 返回值类型:

    enum class future_status {
        ready,   // 结果就绪
        timeout, // 超时
        deferred // 函数未执行
    };
    (C++11)
    
    
  1. wait_until
  • 等待直至某个时间点。结果可用或超时均返回。

  • 时间点,请参见此前文章 C++11 time_point

    template< class Clock, class Duration >
    std::future_status wait_until( 
    const std::chrono::time_point<Clock,Duration>& timeout_time ) const; (C++11)
    
三 例子
#include <iostream>
#include <future>
#include <thread>
 
int main() {
  // 来自 packaged_task 的 future
  std::packaged_task<int()> task([]() { return 7; });  // 包装函数
  std::future<int> f1 = task.get_future();             // 获取 future
  std::thread(std::move(task)).detach(); 

  // 来自 async() 的 future
  std::future<int> f2 = std::async(std::launch::async, []() { return 8; });

  // 来自 promise 的 future
  std::promise<int> p;
  std::future<int> f3 = p.get_future();
  std::thread([&p] { p.set_value_at_thread_exit(9); }).detach();

  std::cout << "valid: " << f1.valid() << ' ' << f2.valid() << ' ' 
                         << f3.valid() << std::endl;
  f1.wait();
  f2.wait();
  f3.wait();
  std::cout << "Results are: " << f1.get() << ' ' << f2.get() << ' ' 
                               << f3.get() << std::endl;
  std::cout << "after get, valid: " << f1.valid() << ' ' << f2.valid() << ' ' 
                                    << f3.valid() << std::endl;

  std::future<int> f4 = std::async(std::launch::async, []() { return 8; }); 
  std::cout << "f4 valid()= " << f4.valid() << std::endl;
  auto fs = f4.share();
  std::cout << "after share, f4 valid()= " << f4.valid() << std::endl;

  std::future<int> f5;
  auto fs1 = f5.share(); // NULL
  std::cin.get();
  return 0;
}
  • 结果
valid: 1 1 1
Results are: 7 8 9
after get, valid: 0 0 0
f4 valid()= 1
after share, f4 valid()= 0
四 shared_future
  1. shared_future 与 future作用相同。

  2. 不同在于,shared_future可复制,并且多个 shared_future可关联于同一个共享状态。

    // 构造函数
    shared_future() noexcept; (1)(C++11)	
    shared_future( const shared_future& other ); (2)(C++11)(C++17)
    shared_future( const shared_future& other ) noexcept; (2)(C++17)
    shared_future( std::future<T>&& other ) noexcept; (3)(C++11)
    shared_future( shared_future&& other ) noexcept; (4)(C++11)
    
  3. get可调用多次

  4. 两种使用方式。

    std::shared_future<int> fs = std::async([]() { return 8; });
    std::cout << "Results: " << fs.get() << ' ' << fs.get() << std::endl;
    
    auto fs1 = std::async([]() { return 8; }).share();
    std::cout << "Results: " << fs1.get() << ' ' << fs1.get() << std::endl;
    
  • 结果

    Results: 8 8
    Results: 8 8
    
五 参考

cppreference- funture

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值