名前空間
変種
操作

std::promise<R>::set_exception

提供: cppreference.com
< cpp‎ | thread‎ | promise
 
 
スレッドサポートライブラリ
スレッド
(C++11)
(C++20)
(C++20)
this_thread 名前空間
(C++11)
(C++11)
(C++11)
相互排他
(C++11)
汎用ロック管理
(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)
(C++11)
(C++11)
条件変数
(C++11)
セマフォ
ラッチとバリア
(C++20)
(C++20)
フューチャー
(C++11)
(C++11)
(C++11)
(C++11)
 
 
void set_exception( std::exception_ptr p );
(C++11以上)

アトミックに例外ポインタ p を共有状態に格納し準備完了にします。

操作は set_valueset_exceptionset_value_at_thread_exitset_exception_at_thread_exit が promise オブジェクトを更新する間 promise オブジェクトに紐付けられている単一のミューテックスを取得するかのように動作します。

共有状態がない、または共有状態がすでに値や例外を格納している場合は、例外が投げられます。

この関数の呼び出しは get_future の呼び出しとのデータ競合を発生しません (それらはお互いに同期する必要はありません)。

目次

[編集] 引数

p - 格納する例外ポインタ。 p がヌルの場合、動作は未定義です。

[編集] 戻り値

(なし)

[編集] 例外

以下の状況で std::future_error が投げられます。

  • *this が共有状態を持っていない。 エラーカテゴリは no_state に設定されます。
  • 共有状態にすでに値や例外が格納されている。 エラーカテゴリは promise_already_satisfied に設定されます。

[編集]

#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 throw
            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() << '\n';
    }
    t.join();
}

出力:

Exception from the thread: Example

[編集] 関連項目

スレッド終了時にのみ通知が配送されるように例外を表す結果を設定します
(パブリックメンバ関数) [edit]