C ++ 11 threads std :: thread :: joinable

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 execution thread checks whether the object identification activity. Specifically, if get_id ()! = Std :: thread :: id () returns true. Therefore, the default configuration of the thread can not be connected.

Code execution has been completed but not yet added to the thread is still considered active thread execution, so you can join.

// 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;
}

operation result:

Published 257 original articles · won praise 22 · views 90000 +

Guess you like

Origin blog.csdn.net/qq_24127015/article/details/104798366