Skip to content

Instantly share code, notes, and snippets.

@paxbun
Created July 8, 2020 01:34
Show Gist options
  • Select an option

  • Save paxbun/8fda4ad6771f19be155918ddd26d2821 to your computer and use it in GitHub Desktop.

Select an option

Save paxbun/8fda4ad6771f19be155918ddd26d2821 to your computer and use it in GitHub Desktop.
Runs program on specific CPU core on Windows using SetProcessAffinityMask
#include <Windows.h>
#include <ctime>
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
HANDLE process = GetCurrentProcess();
const int num_cores = 8;
const int interval = 5000;
for (int i = 0; i < num_cores; ++i)
{
DWORD_PTR mask = 1 << i;
BOOL success = SetProcessAffinityMask(process, mask);
if (!success)
{
cout << "Failed!" << endl;
return 1;
}
cout << i << " started" << endl;
clock_t start = clock();
int garbage = 0;
while (clock() - start < interval)
;
cout << i << " ended" << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment