I was wondering how to force a program in C++ in Visual Studio, to run on specific core/cores (on computers that have more than one).
I found this article, but it refers to C in Linux (and I am using Visual Studio on Windows)
Also, does the Windows version I'm using, matter?
It is possible to use the Windows API function SetThreadIdealProcessorEx()
. This function is applicable to Windows 7 or later. On older systems, it is possible to use SetThreadIdealProcessor()
, albeit with some more limitations.
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.
Read the documentation for these functions carefully, because the system itself can impose limits on which processors a process may run.