Skip to content

Latest commit

 

History

History
93 lines (69 loc) · 2.97 KB

make_ready_at_thread_exit.md

File metadata and controls

93 lines (69 loc) · 2.97 KB

make_ready_at_thread_exit

  • future[meta header]
  • std[meta namespace]
  • packaged_task[meta class]
  • function[meta id-type]
  • cpp11[meta cpp]
void make_ready_at_thread_exit(ArgTypes... args);

概要

タスクを実行し、スレッド終了時に準備完了状態にする。

この関数は、タスクの実行と準備完了状態にするタイミングをずらし、準備完了にするのをスレッド終了時まで遅延させたい場合に使用する。

効果

メンバ変数として保持している関数オブジェクトfに対してINVOKE(f, args..., R)によって関数呼び出しを行い、その戻り値をfutureとの共有状態に格納する。関数fの内部で例外が送出された場合は、共有状態に送出された例外が格納される。

現在のスレッドが終了し、スレッドローカル記憶域を持つ全てのオブジェクトを破棄したあと、準備完了状態(future_status::ready)にする。

戻り値

なし

例外

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

  • promise_already_satisfied : 格納されたタスクがすでに実行された
  • no_state*thisが共有状態を持っていない(packaged_taskオブジェクトがムーブされると起こりうる)

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

int plus_task(int a, int b)
{
  return a + b;
}

// packaged_taskを左辺値参照にしているのはVisual C++ 2012のバグのため
// https://connect.microsoft.com/VisualStudio/feedback/details/737812
void calc(std::packaged_task<int(int, int)>& task)
{
  // 現在のスレッド終了時に準備完了状態にする
  task.make_ready_at_thread_exit(2, 3);
}

int main()
{
  std::packaged_task<int(int, int)> task(plus_task);
  std::future<int> f = task.get_future();

  std::thread t(calc, std::ref(task));
  t.detach();

  // タスクの結果を取得
  int result = f.get();
  std::cout << result << std::endl;
}

出力

5

バージョン

言語

  • C++11

処理系

参照