void reset();
概要
共有状態を作り直す。
一度タスクを実行したのちは共有状態への結果値の再格納ができないため、同じpackaged_taskオブジェクトの同じ関数(タスク)を再度非同期実行したい場合に使用する。
効果
メンバ変数として保持している、非同期実行する関数オブジェクトをfとして、
-
C++11: あたかも以下のように動作する
*this = packaged_task(std::move(f));- この操作によって、新たに共有状態を作成する。古い共有状態は放棄される(詳細は
operator=を参照)。
- この操作によって、新たに共有状態を作成する。古い共有状態は放棄される(詳細は
-
C++26: 以下と等価
if (!valid()) throw future_error(future_errc::no_state); *this = packaged_task(allocator_arg, a, std::move(f));
戻り値
なし
例外
この関数は、以下の例外を送出する可能性がある:
package_taskのコンストラクタ、またはタスク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
処理系
- Clang: ??
- GCC: 4.7.0 ✅
- ICC: ??
- Visual C++: 2012 ✅
参照
- P3503R3 Make type-erased allocator use in
promiseandpackaged_taskconsistent- C++26でアロケータを受け取るコンストラクタで再構築するよう変更された