C++获取线程ID的两种方式

1、获取当前线程的线程ID
2、获取声明的线程变量的线程ID

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <thread>
#include <windows.h>
using namespace std;

void hello()
{
	Sleep(2000);
	cout << "hello world!" << endl;
}

int main()
{
	thread t(hello);
	thread::id thread_id = t.get_id();	// 获取特定线程的id
	thread::id this_thread_id = this_thread::get_id(); // 获取本线程的id

	cout << "thread id = " << thread_id << endl;
	cout << "this thread id = " << this_thread_id << endl;

	t.join();
	return 0;
}

谢谢阅读


版权声明:本文为weixin_43869898原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。