在C ++中的两个不同核心中创建两个线程 [英] make two thread in two different core in C++

查看:115
本文介绍了在C ++中的两个不同核心中创建两个线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了一些有关此问题的文章.案件似乎并不容易.我想在两个核心中创建两个不同的线程.我读的文章太复杂了.我什至无法根据这些文章创建一个简单的线程.另一方面,我需要了解一台计算机必须使用多少个内核才能同时使用两个内核或仅在一个内核上创建线程.我想在Windows上做它们. 任何帮助将非常欢迎

I read some articles about this problem. The case seems not easy. I want to create two different thread in two core. the articles that I read were too complicated. I even couldn't create a simple thread based on those articles. at the other hand I need to understand how many cores a computer have to make available as a choice for using both core or just create threads on one core. I want to do them on windows. Any help would highly welcome

P.S:我不是C ++专业人士.因此,如果可以的话,请用更简单的方法指导我.

P.S: I am not that pro on C++. So please if it is possible guide me with easier ways.

推荐答案

尽管通常这是一个糟糕的主意(线程调度程序通常本身可以做得更好),但基本思路是:

Though it's generally a lousy idea (the thread scheduler usually does a better job on its own), the basic idea would be:

HANDLE thread_a = CreateThread(/* ... */);
HANDLE thread_b = CreateThread(/* ... */);

SetThreadAffinityMask(thread_a, 1 << 0);
SetThreadAffinityMask(thread_b, 1 << 1);

请注意,1<<01<<1分别可以分别与12一样容易地编写-我只是想明确指出每个位代表一个处理器的事实.

Note that the 1<<0 and 1<<1 could just as easily be written as 1 and 2 respectively -- I was just trying to make explicit the fact that each bit position represents a processor.

这篇关于在C ++中的两个不同核心中创建两个线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆