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;
}
谢谢阅读
本文介绍了一个使用C++获取线程ID的示例程序。通过thread库的get_id()方法,可以轻松地获取当前线程和指定线程的ID。此代码示例展示了如何创建一个线程,然后获取并打印该线程及其当前线程的ID。
2399

被折叠的 条评论
为什么被折叠?



