一 前面
- C++11开始,对并发提供了强有力的支持,本文介绍其中的 future 和 shared_future。
二 future
- 类模板 std::future 提供访问异步操作结果的机制。
- 通常与 std::async、std::packaged_task、std::promise等搭配使用。
三 成员函数
-
构造函数
future() noexcept; (1)(C++11 起) future( future&& other ) noexcept; (2)(C++11 起) future( const future& other ) = delete; (3)(C++11 起)
- 即 future 不可复制构造,这也体现在 operator= 函数中。
-
operator=
future& operator=( future&& other ) noexcept; (1)(C++11 起) future& operator=( const future& other ) = delete; (2)(C++11 起) -
share
std::shared_future<T> share() noexcept;
- 转移共享状态到一个 std::shared_future对象中。若valid() = false,返回空。
- 调用后 valid() 为 false。
- get
- 等待异步操作结束并获取结果。
- 调用后 valid() 为 false。
- 若 valid() = false , 行为未定义。即仅可调用一次。
- valid
- std::future 对象是否有效,即是否与某个共享状态相关联。
- 非默认构造或从 std::promise::get_future()、std::packaged_task::get_future() 、std::async()得到的 future对象在第一次调用 get() 或 shared()前是有效的。
- wait
- 等待直至结果可用。
-
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 起)
- 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
-
shared_future 与 future作用相同。
-
不同在于,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 起) -
get可调用多次。
-
两种使用方式。
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

被折叠的 条评论
为什么被折叠?



