在 windows 中的特定核心上制作 C++ 程序 运行

make a c++ program run on a specific core in windows

我想知道如何在 Visual Studio、运行 中在特定 core/cores 中强制使用 C++ 程序(在拥有多个计算机的计算机上)。

我找到了 this 文章,但在 Linux 中引用了 C(我在 windows 上使用 Visual Studio)

此外,我使用的 windows 版本有影响吗?

可以使用 Windows API 函数 SetThreadIdealProcessorEx(). This function is applicable to Windows 7 or later. On older systems, it is possible to use SetThreadIdealProcessor(),尽管有一些限制。

这个,根据第一个的备注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.

我不知道有任何函数会强制调度程序 运行 指定处理器上的线程。因此,给出调度程序将尝试执行的提示可能是最接近满足要求的方法。

最好也使用 SetProcessorAffinityMask(),它可以指定进程可以 运行 的处理器,因为线程似乎不太可能 运行 在不在其父进程的关联掩码内的处理器上。

仔细阅读这些函数的文档,因为系统本身可以对进程可以使用的处理器施加限制运行。