task_scheduler_handle
[scheduler.task_scheduler_handle]
oneapi::tbb::task_scheduler_handle クラスと oneapi::tbb::finalize 関数を使用すると、ユーザーはワーカースレッドの完了を待機できます。
oneapi::tbb::finalize 関数が oneapi::tbb::task_scheduler_handle インスタンスで呼び出されると、ライブラリーによって暗黙的に生成されたすべてのワーカースレッドが完了するまで、呼び出しスレッドをブロックします。
// <oneapi/tbb/global_control.h> ヘッダーで定義
namespace oneapi {
namespace tbb {
class task_scheduler_handle {
public:
task_scheduler_handle() = default;
task_scheduler_handle(oneapi::tbb::attach);
~task_scheduler_handle();
task_scheduler_handle(const task_scheduler_handle& other) = delete;
task_scheduler_handle(task_scheduler_handle&& other) noexcept;
task_scheduler_handle& operator=(const task_scheduler_handle& other) = delete;
task_scheduler_handle& operator=(task_scheduler_handle&& other) noexcept;
explicit operator bool() const noexcept;
void release();
};
void finalize(task_scheduler_handle& handle);
bool finalize(task_scheduler_handle& handle, const std::nothrow_t&) noexcept;
} // namespace tbb
} // namespace oneapiメンバー関数
- task_scheduler_handle()
効果: タスク・スケジューラーへの参照を含まない、
task_scheduler_handleクラスの空のインスタンスを作成します。
- task_scheduler_handle(oneapi::tbb::attach)
効果: 早期の破棄を防止するタスク・スケジューラーへの参照を持つ
task_scheduler_handleクラスのインスタンスを作成します。
- ~task_scheduler_handle()
効果:
task_scheduler_handleクラスのインスタンスを破棄します。空でない場合、タスク・スケジューラーへの参照を解放し、task_scheduler_handleクラスのインスタンスを非アクティブにします。
- task_scheduler_handle(task_scheduler_handle &&other) noexcept
効果:
otherのタスクタスク・スケジューラーが参照するtask_scheduler_handleクラスのインスタンスを作成します。次にotherはタスク・スケジューラーの参照を解放します。
- task_scheduler_handle &operator=(task_scheduler_handle &&other) noexcept
効果: 空でない場合、参照されるタスク・スケジューラーへの参照を解放します。空でない場合、
otherで参照されるタスク・スケジューラーへの参照を追加します。次にotherはタスク・スケジューラーの参照を解放します。戻り値:*thisの参照を返します。
- explicit operator bool() const noexcept
戻り値:
thisが空でなく何らかのタスク・スケジューラーを参照する場合はtrueを、それ以外はfalseを返します。
- void release()
効果: 空でない場合、タスク・スケジューラーへの参照を解放し、
task_scheduler_handleクラスのインスタンスを非アクティブにします。それ以外は何も行いません。非ブロッキング・メソッド
非メンバー関数
- void finalize(task_scheduler_handle &handle)
効果:
handleが空でない場合、すべてのワーカースレッドが完了するまでプログラムの実行をブロックします。それ以外は何もしません。ワーカースレッドの完了を待機することが安全でない場合は、oneapi::tbb::unsafe_wait例外をスローします。
ファイナライズを成功させるには、次の条件を満たす必要があります。
プログラム全体で
task_arenaクラスのアクティブな、または終了していないインスタンスが存在しないこと。task_scheduler_handle::releaseは、異なるアプリケーション・スレッドによってtask_scheduler_handleクラスのほかのアクティブなインスタンスごとに呼び出されます。
これらの条件下では、少なくても 1 つのファイナライズ呼び出しが成功することが保証され、その時点ですべてのワーカースレッドが完了します。呼び出しが同時に行われると、複数の呼び出しが成功する場合があります。
注
プログラム内にアクティブな task_scheduler_handle インスタンスがいくつ存在するか判明している場合、最後のインスタンスを除くすべてを解放 し、最後のインスタンスに対しファイナライズを呼び出す必要があります。
警告
このメソッドは、タスク、並列アルゴリズム、またはフロー・グラフ・ノード内で呼び出されると常に失敗します。
- bool finalize(task_scheduler_handle &handle, const std::nothrow_t&) noexcept
効果:
handleが空でない場合、すべてのワーカースレッドが完了するまでプログラムの実行をブロックします。それ以外は何もしません。動作はファイナライズ(ハンドル)と同じですが、例外に代わってfalseが返され、例外がない場合はtrueが返されます。
例
#include <oneapi/tbb/global_control.h>
#include <oneapi/tbb/parallel_for.h>
#include <iostream>
int main() {
oneapi::tbb::task_scheduler_handle handle;
handle = oneapi::tbb::task_scheduler_handle{oneapi::tbb::attach{}};
// Do some parallel work here, e.g.
oneapi::tbb::parallel_for(0, 10000, [](int){});
try {
oneapi::tbb::finalize(handle);
// oneTBB worker threads are terminated at this point.
} catch (const oneapi::tbb::unsafe_wait&) {
std::cerr << "Failed to terminate the worker threads." << std::endl;
}
return 0;
}参照:
