std::promise<R>::set_exception
提供: cppreference.com
void set_exception(std::exception_ptr p ); | (C++11以上) | |
アトミックに例外ポインタ p
を共有状態に格納し準備完了にします。
操作は set_value、set_exception
、set_value_at_thread_exit、set_exception_at_thread_exit が promise オブジェクトを更新する間 promise オブジェクトに紐付けられている単一のミューテックスを取得するかのように動作します。
共有状態がない、または共有状態がすでに値や例外を格納している場合は、例外が投げられます。
この関数の呼び出しは get_future の呼び出しとのデータ競合を発生しません (それらはお互いに同期する必要はありません)。
目次 |
[編集]引数
p | - | 格納する例外ポインタ。 p がヌルの場合、動作は未定義です。 |
[編集]戻り値
(なし)
[編集]例外
以下の状況で std::future_error が投げられます。
- *this が共有状態を持っていない。 エラーカテゴリは no_state に設定されます。
- 共有状態にすでに値や例外が格納されている。 エラーカテゴリは promise_already_satisfied に設定されます。
[編集]例
Run this code
#include <thread>#include <iostream>#include <future> int main(){std::promise<int> p;std::future<int> f = p.get_future(); std::thread t([&p]{try{// code that may throwthrowstd::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(conststd::exception& e){std::cout<<"Exception from the thread: "<< e.what()<<'\n';} t.join();}
出力:
Exception from the thread: Example
[編集]関連項目
スレッド終了時にのみ通知が配送されるように例外を表す結果を設定します (パブリックメンバ関数) |