i really have a hard time understanding the SetThreadAffinityMask function. Im trying to implement a timer with the QueryPerformanceCounter function but I dont understand how to get the thread affinity right. A guy on msdn posted the following code example:
void HRTimer::StartTimer(void)
{
DWORD_PTR oldmask = ::SetThreadAffinityMask(::GetCurrentThread(), 0);
::QueryPerformanceCounter(&start);
::SetThreadAffinityMask(::GetCurrentThread(), oldmask);
}
But when I adopt this code snippet the value for oldmask returned by SetThreadAffinityMask is zero. On MSDN I saw that a return value of zero means that an error occurred. I called GetLastError() and got the error code for ERROR_INVALID_PARAMETER. Now im wondering if the code snippet above isnt correct. Can someone please explain me how to use SetThreadAffinityMask correctly so that QueryPerformanceCounter is for example only called on the first CPU on the system? Or is the above example correct allthough SetThreadAffinityMask returns zero?
Thank you in advance.
The mask is a bitfield: each bit designate a processor. 0 means “no processor”. It’s not logic.
MSDN for SetThreadAffinityMask