Created
July 8, 2020 01:34
-
-
Save paxbun/8fda4ad6771f19be155918ddd26d2821 to your computer and use it in GitHub Desktop.
Runs program on specific CPU core on Windows using SetProcessAffinityMask
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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