void join();
|
(C++11 起) | |
阻塞当前线程,直至 *this 所标识的线程完成其执行。
*this 所标识的线程的完成同步于从 join() 的成功返回。
目录 |
(无)
(无)
joinable 为 false
若错误发生则为 std::system_error 。
#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!
容许线程从线程句柄独立开来执行 (公开成员函数) |
|
检查线程是否可合并,即潜在地运行于平行环境中 (公开成员函数) |
|
thrd_join的 C 文档
|