std::thread::join

void join();
(C++11 起)

阻塞当前线程,直至 *this 所标识的线程完成其执行。

*this 所标识的线程的完成同步于join() 的成功返回。

目录

参数

(无)

返回值

(无)

后置条件

joinablefalse

异常

若错误发生则为 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!

引用

  • 30.3.1.5 thread members [thread.thread.member]

参阅

容许线程从线程句柄独立开来执行
(公开成员函数)
检查线程是否可合并,即潜在地运行于平行环境中
(公开成员函数)
thrd_joinC 文档

版本历史

  • (当前 | 先前 2017年8月25日 (五) 06:03Fruderica讨论 | 贡献. . (1,763字节) (+70). . (撤销)
  • 当前 | 先前 2017年6月13日 (二) 00:17222.210.113.74讨论. . (1,693字节) (+8). . (撤销)
  • 当前 | 先前 2013年7月2日 (二) 11:35P12bot讨论 | 贡献 . . (1,685字节) (-38). . (Use {{lc}}. Update links. Various fixes.) (撤销)
  • 当前 | 先前 2012年11月2日 (五) 18:24P12bot讨论 | 贡献 . . (1,723字节) (+241). . (r2.7.3) (机器人添加:de, en, es, fr, it, ja, pt, ru) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 03:32P12讨论 | 贡献 . . (1,482字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前) 2012年10月25日 (四) 12:00TranslationBot讨论 | 贡献. . (1,482字节) (+1,482). . (Translated from the English version using Google Translate)