#include "stdafx.h"
#include <iostream>
#include <vector>
#include <mutex>
#include <windows.h>
using namespace std;
DWORD WINAPI threadProc(LPVOID p)
{
HANDLE process = GetCurrentProcess();
DWORD_PTR processAffinityMask = (1 << 1 | 1 << 2); //use processor 1 and 2
BOOL success = SetProcessAffinityMask(process, processAffinityMask);
clock_t t = clock() + 100; // consume 100ms ...
while (clock() < t) {} // of CPU time
printf("thread %d:\tlogical CPU %d\tProcessId %d\n", (int)p, GetCurrentProcessorNumber(), GetCurrentProcessId());
return 0;
}
int main()
{
unsigned num_cpus = std::thread::hardware_concurrency(); //retrieve the number of processor
std::cout << "Launching " << num_cpus << " threads\n";
for (int i = 0; i < num_cpus; i++)
{
HANDLE h = CreateThread(NULL, 0, threadProc, (LPVOID)i, 0, NULL);
}
getchar();
return 0;
}
28 December 2018
C++ set processor affinity on Windows
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment