我想知道如何强制 Visual Studio 中的 C++ 程序在特定的一个或多个核心上运行(在拥有多个核心的计算机上)。
我发现this文章,但 in 指的是 Linux 中的 C(我在 Windows 上使用 Visual Studio)
另外,我使用的 Windows 版本重要吗?
最佳答案
可以使用Windows API函数SetThreadIdealProcessorEx()
。此功能适用于Windows 7或更高版本。在较旧的系统上,可以使用 SetThreadIdealProcessor()
,尽管还有一些限制。
这是根据第一个链接的注释
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.
我不知道有任何函数强制调度程序在指定处理器上运行线程。因此,给出一个提示(调度程序将尝试执行该提示)可能是最接近满足您的要求的方法。
建议也使用 SetProcessorAffinityMask()
同样,它可以指定进程可以运行的处理器,因为线程似乎不太可能在不在其父进程亲和性掩码内的处理器上运行。
仔细阅读这些函数的文档,因为系统本身可以对进程可以运行的处理器施加限制。
关于c++ - 使c++程序在windows中的特定核心上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40068025/