C++11 的線程庫問題

emyueguang 2014-11-10 12:21:34
使用C++11的線程庫,這個地方很不理解,為什麼獲取到的線程ID都是一樣的呢


這是結果,前面一個數值是使用std::this_thread::get_id()獲取到的值,後面一個是使用GetCurrentThreadId()獲取到的,可以看出來,計算結果真真切切的是三個不同的線程在做,但是使用C++11線程庫獲取到的線程ID有的卻是一樣的。為什麼呢?
...全文
89 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
ri_aje 2014-11-10
  • 打赏
  • 举报
回复
代码呢???
ri_aje 2014-11-10
  • 打赏
  • 举报
回复
引用 2 楼 emyueguang 的回复:
#include <thread>
#include "win32_sem.h"

uint32_t	sem = create_semaphore(0, INT32_MAX);

void process_packet()
{
	printf("thread :%d	begin process data. \n", std::this_thread::get_id());
	for (uint64_t i = 0; i < UINT64_MAX; ++i);
	printf("thread: %d	process data over. \n", std::this_thread::get_id());
}

void extract_packet()
{	
	while (true)
	{		
		printf("thread:%d - %d wait data coming ... \n", std::this_thread::get_id(), ::GetCurrentThreadId());
		wait_semaphore(sem, INFINITE);
		printf("thread:%d - %d receive data. \n", std::this_thread::get_id(), ::GetCurrentThreadId());

		// get data from array

		process_packet();
	}
}

void test_cpp_thread()
{
	std::thread	t(extract_packet);
	t.detach();
	std::thread	t2(extract_packet);
	t2.detach();
	std::thread	t3(extract_packet);
	t3.detach();
		
	release_semaphore(sem, 3);	

	std::this_thread::sleep_for(std::chrono::seconds(5));

	release_semaphore(sem, 1);

	std::this_thread::sleep_for(std::chrono::seconds(5));
	
	release_semaphore(sem, 1);
}

int main()
{	
	test_cpp_thread();
	system("pause");
	return 0;
}
懒得为了试这个还跑到 windows 底下。写了个例子:

#include <iostream>
#include <thread>

void function ()
{
 std::cout << std::this_thread::get_id() << std::endl;
}

int main ()
{
 std::thread    t(function);
 std::thread    t2(function);
 std::thread    t3(function);
 t .join();
 t2.join();
 t3.join();
}
试了一下没问题,输出比如: 140526880892672 140526872499968 140526864107264 可以看到三个不同的线程,你试试在你哪里一样吗。 另外,std::thread::id 应该用 std::cout 输出,printf 很可能给搞错。
emyueguang 2014-11-10
  • 打赏
  • 举报
回复
#include <thread>
#include "win32_sem.h"

uint32_t	sem = create_semaphore(0, INT32_MAX);

void process_packet()
{
	printf("thread :%d	begin process data. \n", std::this_thread::get_id());
	for (uint64_t i = 0; i < UINT64_MAX; ++i);
	printf("thread: %d	process data over. \n", std::this_thread::get_id());
}

void extract_packet()
{	
	while (true)
	{		
		printf("thread:%d - %d wait data coming ... \n", std::this_thread::get_id(), ::GetCurrentThreadId());
		wait_semaphore(sem, INFINITE);
		printf("thread:%d - %d receive data. \n", std::this_thread::get_id(), ::GetCurrentThreadId());

		// get data from array

		process_packet();
	}
}

void test_cpp_thread()
{
	std::thread	t(extract_packet);
	t.detach();
	std::thread	t2(extract_packet);
	t2.detach();
	std::thread	t3(extract_packet);
	t3.detach();
		
	release_semaphore(sem, 3);	

	std::this_thread::sleep_for(std::chrono::seconds(5));

	release_semaphore(sem, 1);

	std::this_thread::sleep_for(std::chrono::seconds(5));
	
	release_semaphore(sem, 1);
}

int main()
{	
	test_cpp_thread();
	system("pause");
	return 0;
}

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧