C++ 线程 获取线程id

本秂侑毒 提交于 2020-03-11 16:30:46
#include "stdafx.h"
#include <iostream>
#include <thread>
#include <string>
#include <chrono>
#include <mutex>
#include <windows.h>
using namespace std;

std::mutex g_display_mutex;
void foo()
{
	std::thread::id this_id = std::this_thread::get_id();
	unsigned int t = *(unsigned int*)&this_id;// threadid 转成 unsigned int
	unsigned int threadid = t;
	g_display_mutex.lock();
	std::cout << "thread" << this_id << " sleeping...\n";
	std::cout << "t = " << threadid << endl;
	g_display_mutex.unlock();
	std::this_thread::sleep_for(std::chrono::seconds(1));
}

int main()
{
	std::thread t1(foo);
	std::thread t2(foo);
	t1.join();
	t2.join();
    return 0;
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!