void set_exception_at_thread_exit(exception_ptr p);
概要
スレッド終了時に結果の例外を設定する
効果
例外ポインタ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();
}
xxxxxxxxxx
#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 (...) {
// 呼び出し元スレッドに例外を設定し、
// このスレッド終了時に準備完了状態にする
出力
invalid argument!
バージョン
言語
- C++11
処理系
- Clang: 3.0 ✅
- GCC: 5.0 ✅
- ICC: ??
- Visual C++: 2012 ✅