简体   繁体   English

使一个c ++程序在Windows中的特定核心上运行

[英]make a c++ program run on a specific core in windows

i was wondering how to force a program in C++ in Visual Studio, run on specific core/cores (on computers who has more than one). 我想知道如何在Visual Studio中强制使用C ++编写程序,在特定的核心/核心上运行(在具有多个核心/核心的计算机上)。

i found this article, but in refers to C in Linux (and i am using Visual Studio on windows) 我发现这篇文章,但在Linux中引用C(我在Windows上使用Visual Studio)

also, does the windows version I'm using, matter? 我也在使用Windows版本吗?

It is possible to use the Windows API function SetThreadIdealProcessorEx() . 可以使用Windows API函数SetThreadIdealProcessorEx() This function is applicable to Windows 7 or later. 此功能适用于Windows 7或更高版本。 On older systems, it is possible to use SetThreadIdealProcessor() , albeit with some more limitations. 在较旧的系统上,可以使用SetThreadIdealProcessor() ,尽管有一些限制。

This, according to remarks at the first link 这是根据第一个链接的评论

Specifying a thread ideal processor provides a hint to the scheduler about the preferred processor for a thread. 指定线程理想处理器为调度程序提供有关线程的首选处理器的提示。 The scheduler runs the thread on the thread's ideal processor when possible. 调度程序尽可能在线程的理想处理器上运行线程。

I'm not aware of any function that forces the scheduler to run a thread on a specified processor. 我不知道任何强制调度程序在指定处理器上运行线程的函数。 So giving a hint, which the scheduler will attempt to act on, is probably the closest you can get to meeting your requirement. 因此,提供调度程序将尝试操作的提示可能是您可以满足您的要求的最接近的提示。

It would probably be advisable to also use SetProcessorAffinityMask() as well, which works to specify processors on which a process may run, since it would seem unlikely that a thread can run on a processor that is not within its parent process's affinity mask. 建议也可以使用SetProcessorAffinityMask() ,它可以指定进程可以运行的处理器,因为线程似乎不可能在不在其父进程的关联掩码内的处理器上运行。

Read the documentation for these functions carefully, because the system itself can impose limits on which processors a process may run. 请仔细阅读这些功能的文档,因为系统本身可以对进程可能运行的处理器施加限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM