引言
HANDLE thread = GetCurrentThread(); // Set affinity to the first core DWORD_PTR oldMask = SetThreadAffinityMask(thread, GTimerMask ); // Query the timer QueryPerformanceCounter(&mStartTime); mStartTick = GetTickCount(); // Reset affinity SetThreadAffinityMask(thread, oldMask);使用前,需要重置GTimerMask:
void appResetTimerMask()
{
// Get the current process core mask
ULONG_PTR proMask;
ULONG_PTR sysMask;
GetProcessAffinityMask(GetCurrentProcess(), &procMask, &sysMask);
// If procMask is 0, consider there is only one core available
// (using 0 as procMask will cause an infinite loop below)
if (procMask == 0)
procMask = 1;
// Find the lowest core that this process uses
if (GTimerMask == 0)
{
GTimeMask = 1;
while ((GTimerMask & procMask) == 0)
{
GTimerMask <<= 1;
}
}
}完整的使用方法:inline DOUBLE appSeconds()
{
LARGE_INTEGER Cycles;
HANDLE thread = NULL;
ULONG_PTR oldMask = 0;
if (GEnableTimerAffinityMask) //GEnableTimerAffinityMask为TRUE时需要将线程绑定到固定核心
{
thread = GetCurrentThread();
if (GTimerMask == 0) //GTimerMask默认值为0
{
appResetTimerMask();
}
oldMask = SetThreadAffinityMask(thread, GTimerMask);
}
QueryPerformanceCounter(&Cycles);
if (GEnableTimerAffinityMask)
{
SetThreadAffinityMask(thread, oldMask);
}
return Cycles.QuadPart * GSecondsPerCycle + 16777216.0;
}高精度计时器QueryPerformanceCounter正确的打开方式(windows环境下)
原文:http://blog.csdn.net/coffeecato/article/details/44656001