std::thread::joinable

bool joinable() const noexcept;
(C++11 起)

检查 thread 对象是否标识活跃的执行线程。具体是返回 true if get_id() != std::thread::id() 。故默认构造的 thread 不可合并。

完成执行代码,但未被合并的线程仍被认为是活跃线程,从而可合并。

目录

参数

(无)

返回值

若 thread 对象标识活跃的执行线程则为 true ,否则为 false

示例

#include <iostream>
#include <thread>
#include <chrono>
 
void foo()
{
    std::this_thread::sleep_for(std::chrono::seconds(1));
}
 
int main()
{
    std::thread t;
    std::cout << "before starting, joinable: " << t.joinable() << '\n';
 
    t = std::thread(foo);
    std::cout << "after starting, joinable: " << t.joinable() << '\n';
 
    t.join();
    std::cout << "after joining, joinable: " << t.joinable() << '\n';
}

输出:

before starting, joinable: 0
after starting, joinable: 1
after joining, joinable: 0

参阅

返回线程的 id
(公开成员函数)
等待线程完成其执行
(公开成员函数)
容许线程从线程句柄独立开来执行
(公开成员函数)

引用

  • 30.3.1.5 thread members [thread.thread.member]

版本历史

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