最終更新日時(UTC):
が更新

履歴 編集

function
<future>

std::packaged_task::reset(C++11)

void reset();

概要

共有状態を作り直す。

一度タスクを実行したのちは共有状態への結果値の再格納ができないため、同じpackaged_taskオブジェクトの同じ関数(タスク)を再度非同期実行したい場合に使用する。

効果

*this = packaged_task(std::move(f));

によって、新たに共有状態を作成する。古い共有状態は放棄される。

fはメンバ変数として保持している、非同期実行する関数オブジェクト。

戻り値

なし

例外

この関数は、以下の例外を送出する可能性がある:

  • bad_alloc : 新たな共有状態のアロケートに失敗
  • タスクfのムーブコンストラクタが送出するあらゆる例外
  • future_error : 共有状態を持っていない状態でこの関数を呼び出した場合、no_stateをerror conditionにして送出する

#include <iostream>
#include <future>

int calc() { return 3; }

void execute(std::packaged_task<int()>& task)
{
  std::future<int> f = task.get_future(); // 結果値取得のためのfutureを取得
  task();
  std::cout << f.get() << std::endl;
}

int main()
{
  std::packaged_task<int()> task(calc); // calc()を非同期タスクとして登録

  execute(task); // タスクを実行

  task.reset(); // 共有状態を作り直す

  execute(task); // タスクを再度実行
}

出力

3
3

バージョン

言語

  • C++11

処理系

参照