Skip to content

Latest commit

 

History

History
103 lines (80 loc) · 2.98 KB

set_exception_at_thread_exit.md

File metadata and controls

103 lines (80 loc) · 2.98 KB

set_exception_at_thread_exit

  • future[meta header]
  • std[meta namespace]
  • promise[meta class]
  • function[meta id-type]
  • cpp11[meta cpp]
void set_exception_at_thread_exit(exception_ptr p);
  • exception_ptr[link /reference/exception/exception_ptr.md]

概要

スレッド終了時に結果の例外を設定する

効果

例外ポインタpを、すぐに準備完了状態(future_status::ready)にはせずに共有状態に格納する。現在のスレッドが終了し、スレッドローカル記憶域を持つ全てのオブジェクトを破棄したあと、準備完了状態にする。

戻り値

なし

例外

この関数は、以下のerror conditionを持つfuture_error例外オブジェクトを送出する可能性がある:

  • promise_already_satisfied : すでに値もしくは例外が設定されている
  • no_state*thisが共有状態を持っていない(promiseオブジェクトがムーブされると起こりうる)

#include <iostream>
#include <future>
#include <thread>
#include <utility>
#include <stdexcept>
#include <functional>

// promiseを左辺値参照にしているのはVisual C++ 2012のバグのため
// https://connect.microsoft.com/VisualStudio/feedback/details/737812
void calc(std::promise<int>& p)
{
  try {
    // 計算で何らかのエラーが発生した
    throw std::invalid_argument("invalid argument!");
  }
  catch (...) {
    // 呼び出し元スレッドに例外を設定し、
    // このスレッド終了時に準備完了状態にする
    std::exception_ptr ep = std::current_exception();
    p.set_exception_at_thread_exit(ep);
  }
}

int main()
{
  std::promise<int> p;
  std::future<int> f = p.get_future();

  std::thread t(calc, std::ref(p));

  try {
    int result = f.get(); // promiseで設定された例外が送出される
  }
  catch (std::invalid_argument& e) {
    std::cout << e.what() << std::endl;
  }

  t.join();
}

出力

invalid argument!

バージョン

言語

  • C++11

処理系

参照