std::thread::join
来自cppreference.com
| void join(); |
(C++11 起) | |
阻塞当前线程,直至 *this 所标识的线程完成其执行。
*this 所标识的线程的完成同步于从 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
[编辑] 示例
运行此代码
#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::thread helper1(foo); std::cout << "starting second helper...\n"; std::thread 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++11 standard (ISO/IEC 14882:2011):
- 30.3.1.5 thread members [thread.thread.member]
[编辑] 参阅
| 容许线程从线程句柄独立开来执行 (公开成员函数) | |
| 检查线程是否可合并,即潜在地运行于平行环境中 (公开成员函数) | |
| thrd_join 的 C 文档
| |

