std::jthread::join - cppreference.com
提供: cppreference.com
<tbody> </tbody>
*this が表すスレッドが実行を終えるまで現在のスレッドをブロックします。
*this が表すスレッドの完了は対応する join() からの成功の戻りに対して同期します。
*this 自体に対しては同期は行われません。 複数のスレッドから同じ jthread オブジェクトの join() を並行的に呼ぶことは競合状態を構成し、未定義動作となります。
引数
(なし)
戻り値
(なし)
事後条件
joinable() が false である。
例外
エラーが発生した場合は std::system_error。
エラーコンディション
this->get_id() == std::this_thread::get_id()(デッドロック) の場合は std::errc- スレッドが有効でない場合は std::errc。
- joinable() が
falseの場合は std::errc。
例
#include <iostream> #include <thread> #include <chrono> void foo() { // 何か重い処理をしているつもり std::this_thread::sleep_for(std::chrono::seconds(1)); } void bar() { // 何か重い処理をしているつもり std::this_thread::sleep_for(std::chrono::seconds(1)); } int main() { std::cout << "starting first helper...\n"; std::jthread helper1(foo); std::cout << "starting second helper...\n"; std::jthread helper2(bar); std::cout << "waiting for helpers to finish..." << std::endl; helper1.join(); helper2.join(); std::cout << "done!\n"; }
出力:
starting first helper... starting second helper... waiting for helpers to finish... done!
参考文献
- C++20 standard (ISO/IEC 14882:2020):
- 32.4.3.2 Members [thread.jthread.mem]