std::jthread::join
提供: cppreference.com
void join(); |
(C++20以上) | |
*this が表すスレッドが実行を終えるまで現在のスレッドをブロックします。
*this が表すスレッドの完了は対応する join()
からの成功の戻りに対して同期します。
*this 自体に対しては同期は行われません。 複数のスレッドから同じ jthread オブジェクトの join() を並行的に呼ぶことは競合状態を構成し、未定義動作となります。
目次 |
[編集] 引数
(なし)
[編集] 戻り値
(なし)
[編集] 事後条件
joinable() が false である。
[編集] 例外
エラーが発生した場合は std::system_error。
[編集] エラーコンディション
- this->get_id() == std::this_thread::get_id() (デッドロック) の場合は resource_deadlock_would_occur
- スレッドが有効でない場合は no_such_process。
- joinable() が false の場合は invalid_argument。
[編集] 例
Run this code
#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]
[編集] 関連項目
スレッドを jthread オブジェクトから独立して実行できるようにします (パブリックメンバ関数) | |
スレッドが合流可能かどうか、すなわち潜在的に並列文脈で実行中かどうか調べます (パブリックメンバ関数) | |
thrd_join の C言語リファレンス
|