我正在尝试通过win32 API设置std::thread或boost::thread的线程亲和性。我想使用SetThreadAffinityMask函数将每个线程固定到我的机器上的特定核心。
我使用了线程native_handle成员函数获取传递给SetThreadAffinityMask函数的线程句柄。然而,这样做导致SetThreadAffinityMask函数返回0,表示无法设置线程亲和性。
unsigned numCores = std::thread::hardware_concurrency();
std::vector<std::thread> threads(numCores);
for (int i = 0; i < numCores; i++)
{
threads.push_back(std::thread(workLoad, i));
cout << "Original Thread Affinity Mask: " << SetThreadAffinityMask(threads[i].native_handle() , 1 << i) << endl;
}
for (thread& t : threads)
{
if (t.joinable())
t.join();
}
原始线程亲和掩码: 0
原始线程亲和掩码: 0
原始线程亲和掩码: 0
原始线程亲和掩码: 0
原始线程亲和掩码: 0
原始线程亲和掩码: 0
原始线程亲和掩码: 0
...等等
GetLastError()
返回什么错误代码? - Jonathan Potter