std::thread::joinable
Checks if the std::thread
object identifies an active thread of execution. Specifically, returns true if get_id() != std::thread::id(). So a default constructed thread is not joinable.
A thread that has finished executing code, but has not yet been joined is still considered an active thread of execution and is therefore joinable.
检查std :: thread对象是否标识活动的执行线程。 具体来说,如果get_id()!= std :: thread :: id()返回true。 因此,默认构造的线程不可连接。
已经完成执行代码但尚未加入的线程仍被视为执行中的活动线程,因此可以加入。
// threadTest.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <thread>
#include <string>
#include <chrono>
#include <mutex>
using namespace std;
void foo()
{
std::this_thread::sleep_for(std::chrono::seconds(1));
}
int main()
{
std::thread t;
std::cout << "before starting,joinable: " << std::boolalpha << 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";
return 0;
}
运行结果: