I need to pin various c/c++ processes to specific cores on a machine for benchmarking only on Windows 7 64-bit. My machine has 16 cores (2x8). I\'m trying to do this by call
As already mentioned, it's a bitmask. You may want to use the result of GetProcessAffinityMask, incase your process or system doesn't have access to all the cores already. Here's what I came up with.
#include
#include
using namespace std;
int main () {
HANDLE process = GetCurrentProcess();
DWORD_PTR processAffinityMask;
DWORD_PTR systemAffinityMask;
if (!GetProcessAffinityMask(process, &processAffinityMask, &systemAffinityMask))
return -1;
int core = 2; /* set this to the core you want your process to run on */
DWORD_PTR mask=0x1;
for (int bit=0, currentCore=1; bit < 64; bit++)
{
if (mask & processAffinityMask)
{
if (currentCore != core)
{
processAffinityMask &= ~mask;
}
currentCore++;
}
mask = mask << 1;
}
BOOL success = SetProcessAffinityMask(process, processAffinityMask);
cout << success << endl;
return 0;
}