天天看點

C++11 promise

目錄

        • 一 promise
        • 二 定義
        • 三 成員函數
        • 四 get_future
        • 五 設定結果
          • 1. set_value
          • 2. set_value_at_thread_exit
          • 3. set_exception
          • 4. set_exception_at_thread_exit
        • 六 參考

一 promise

  • 前文 C++11 async 可知,異步操作的方式之一,是通過 std::async 接口調用可執行對象,然後通過 std::future 獲得結果。
  • 另一種常見方式是在指定的 std::thread 中執行,如何擷取在 std::thread 中的執行結果?辦法之一就是通過 std::promise。

二 定義

// 頭檔案 <future>
template< class R > class promise; (1)(C++11 起) // 空模闆
template< class R > class promise<R&>; (2)(C++11 起) // 非 void 特化,用于線上程間交流對象
template<>          class promise<void>; (3)(C++11 起) // void 特化,用于交流無狀态事件
           
  • 類模闆 std::promise 提供存儲值或異常的設施。
  • promise 對象與其 get_future() 接口傳回的 future 對象構成 promise-future 交流通道。通道的 promise對象與 future 對象關聯同一共享狀态(shared state), promise 對象是通道的 push 端,future 對象是通道的 pop 端。promise 存儲值或異常到共享狀态中,使共享狀态就緒,future 通過get()異步獲得結果。
  • promise 對共享狀态的三種處理

    使就緒: promise 存儲結果或異常于共享狀态。标記共享狀态為就緒,并解除阻塞任何等待于與該共享狀态關聯的 future 上的線程。

    釋放: promise 放棄其對共享狀态的引用。若這是最後一個這種引用,則銷毀共享狀态。除非這是 std::async 所建立的未就緒的共享狀态,否則此操作不阻塞。

    抛棄: promise 存儲以 std::future_errc::broken_promise 為 error_code 的 std::future_error 類型異常,令共享狀态為就緒,然後釋放它。

  • std::promise 隻應當使用一次。
    Note that the std::promise object is meant to be used only once.

三 成員函數

  1. 構造
    promise(); (1)(C++11 起)
    template< class Alloc >
    promise( std::allocator_arg_t, const Alloc& alloc ); (2)(C++11 起)
    promise( promise&& other ) noexcept; (3)(C++11 起)
    promise( const promise& other ) = delete; (4)(C++11 起) // 不可複制構造
               
  2. 析構
  • 若共享狀态就緒,則釋放它。若共享狀态未就緒,則抛棄。
  1. operator=
    promise& operator=( promise&& other ) noexcept; (1)(C++11 起)
    promise& operator=( const promise& rhs ) = delete; (2)(C++11 起) // 不可複制指派
               

四 get_future

  • 若無共享狀态,或已調用 get_future 則抛出異常。即僅可調用一次。
  • 可通過 get_future 間接獲得結果。

五 設定結果

1. set_value
  • 設定結果為指定值, 并使狀态就緒。
// (僅為泛型 promise 模闆的成員)
void set_value( const R& value ) (1)(C++11 起)
void set_value( R&& value ); (2)(C++11 起)

// (僅為 promise<R&> 模闆特化的成員)
void set_value( R& value ); (3)(C++11 起)
// (僅為 promise<void> 模闆特化的成員)
void set_value(); (4)(C++11 起)
           
  • demo
#include <algorithm>  // sort
#include <future>     // future promise
#include <iostream>   // cout cin endl
#include <iterator>   // istream_iterator back_inserter
#include <sstream>    // istringstream
#include <thread>     // thread
#include <vector>     // vector
#include <numeric>    // accumulate

void accumulate(std::vector<int>::iterator first,
                std::vector<int>::iterator last,
                std::promise<int> accumulate_promise) {
  int sum = std::accumulate(first, last, 0);
  accumulate_promise.set_value(sum);  // 原子地存儲 num 到共享狀态,并令狀态就緒
}
int main() {
  std::istringstream iss_numbers{"3 4 1 42 23 -23 93 2 -289 93"};
  std::vector<int> numbers;
  {
    // set_value (4)
    std::promise<void> numbers_promise;
    std::future<void> numbers_future = numbers_promise.get_future();
   
    std::thread t([&] {
      std::copy(std::istream_iterator<int>{iss_numbers},
                std::istream_iterator<int>{}, std::back_inserter(numbers));
      numbers_promise.set_value(); // 使狀态就緒
    });
   
    numbers_future.wait();
    std::sort(numbers.begin(), numbers.end());
   
    for (int num : numbers)
      std::cout << num << ' ';
    std::cout << std::endl;
   
    t.join();
  }
  {
    // set_value (1)
    std::promise<int> accumulate_promise;
    std::future<int> accumulate_future = accumulate_promise.get_future();
    std::thread t(accumulate, numbers.begin(), numbers.end(),
                            std::move(accumulate_promise));

    std::cout << "result=" << accumulate_future.get() << '\n';
    t.join(); 
  }
}
           
  • 結果
-289 -23 1 2 3 4 23 42 93 93
result=-51
           
2. set_value_at_thread_exit
  • 原子地存儲 value 到共享狀态,而不立即令狀态就緒。在目前線程退出時,銷毀所有擁有線程局域存儲期的對象後,再令狀态就緒。
// (僅為泛型 promise 模闆的成員)
void set_value_at_thread_exit( const R& value );(1)	(C++11 起)
void set_value_at_thread_exit( R&& value );(2)	(C++11 起)

// (僅為 promise<R&> 模闆特化的成員)
void set_value_at_thread_exit( R& value );(3)	(C++11 起)
// (僅為 promise<void> 模闆特化的成員)
void set_value_at_thread_exit();(4)	(C++11 起)
           
  • demo
// set_value_at_thread_exit
using namespace std::chrono_literals;
std::promise<int> p;
std::future<int> f = p.get_future();
std::thread([&p] {
  std::this_thread::sleep_for(1s);
  p.set_value_at_thread_exit(9);
}).detach();

std::cout << "Waiting..." << std::flush;
f.wait();
std::cout << "Done!\nResult is: " << f.get() << std::endl;
           
  • 結果
Waiting...Done!
Result is: 9
           
3. set_exception
  • 存儲異常指針 p 到共享狀态中,并令狀态就緒。
  • demo
// set_exception
std::promise<int> p;
std::future<int> f = p.get_future();

std::thread t([&p] {
  try {
    throw std::runtime_error("Example");
  } catch (...) {
    try {
      // store anything thrown in the promise
      p.set_exception(std::current_exception());
    } catch (...) {
    }  // set_exception() may throw too
  }
});

try {
  std::cout << f.get();
} catch (const std::exception& e) {
  std::cout << "Exception from the thread: " << e.what() << std::endl;
}
t.join();
           
  • 結果
Exception from the thread: Example
           
4. set_exception_at_thread_exit
  • 存儲異常指針 p 到共享狀态中,而不立即使狀态就緒。在目前線程退出時,銷毀所有擁有線程局域存儲期的變量後,再零狀态就緒。

六 參考

cppreference-promise

上一篇: C++11 auto
下一篇: C++11 final