🐭 std::future 与 std::promise

对于异步任务来说,std::future 是给任务调用方用来查询结果的,std::promise 是给任务执行方来填充结果的。它们之间通过一个「shared state」联系在一起,进而完成结果数据的写入与读出。
std::future 操作图解:
std_future.png
代码示例 :

  1. #include <algorithm>
  2. #include <future>
  3. #include <iostream>
  4. #include <numeric>
  5. #include <thread>
  6. #include <vector>
  7. void accumulate(std::vector<int>::iterator first, std::vector<int>::iterator last,
  8. std::promise<int> promise) {
  9. int sum = std::accumulate(first, last, 0);
  10. promise.set_value(sum);
  11. }
  12. int main() {
  13. // using promise<int> to transmit a result between threads
  14. // 1. the result is not store in the std::promise nor std::future, since:
  15. // - callee maybe destroyed before caller calls std::future::get()
  16. // - the std::future maybe copied or moved in caller side
  17. // 2. there must be a shared state between std::promise and std::future
  18. // - we can set value through std::promise
  19. // - and get value through std::future
  20. std::vector<int> nums = {1, 2, 3, 4, 5, 6};
  21. std::promise<int> promise;
  22. std::future<int> future = promise.get_future();
  23. std::thread thread(accumulate, nums.begin(), nums.end(), std::move(promise));
  24. std::cout << "result = " << future.get() << '\n'; // result = 21
  25. thread.join(); // don't forget to join the std::thread
  26. return 0;
  27. }

🐂 std::future 及其析构

在 C++ 中 std::thread 与 std::future 都是异步任务对应的操作系统软件线程的一个抽象。但是析构一个 joinable 的 std::thread 会导致程序异常结束,析构一个 std::future 似乎是一个正常的操作。
这是因为 std::future 的析构与上面提到的「shared state」的状态有关,对应于不同的「shared state」状态的 std::future 其析构方式不一样。
特殊析构操作 (会隐式调用 join),满足条件 1,2,3:

  1. 使用 std::async 创建的 std::future
  2. 线程启动策略为 std::launch::async
  3. 是最后一个 引用到 「shared state」的 std::future

正常析构操作 (只会减小引用计数):

  1. 除了上面说到的其他 std::future (包括 std::shared_future)
  2. 比如 std::packaged_task 创建的 std::future
  3. 比如 std::promise 创建的 std::future

代码示例 :

  1. #include <future>
  2. #include <iostream>
  3. #include <thread>
  4. int sum(int a, int b) { return a + b; }
  5. int main() {
  6. // case 1: std::future special destruction (implicit call join)
  7. // 1. refers to a shared state that was created due to a call to std::async
  8. // 2. task’s launch policy is std::launch::async
  9. // 3. it is the last future referring to the shared state
  10. {
  11. // meet the conditions 1, 2, 3
  12. auto fut1 = std::async(std::launch::async, sum, 1, 2);
  13. std::cout << fut1.get() << '\n'; // 3
  14. } // before fut is destory, will implicit call join on the running thread
  15. // case 2: std::future (fut1) normal destruction
  16. // decrements the reference count inside the shared state
  17. {
  18. std::shared_future<int> fut3;
  19. {
  20. std::shared_future<int> fut2;
  21. {
  22. auto fut1 = std::async(std::launch::async, sum, 1, 2);
  23. fut2 = fut1.share(); // fut1 became invalid (onece)
  24. fut3 = fut2;
  25. } // fut2 is not the last std::future to the shared state, normal destory
  26. }
  27. std::cout << fut3.get() << '\n'; // 3
  28. } // fut3 is the last std::future to the shared state, special destory
  29. return 0;
  30. }

:::tips 这里的 std::shared_future 与 std::shared_ptr 类似是基于引用计数的,能保证「shared state」一直可用,最后一个引用到「shared state」的 std::future 会析构自己,然后「shared state」也会析构 :::

🐯 总结

  1. 理解 std::future 的两种析构过程
  2. 理解 std::futrue 的几种创建方式