最近发现在VMware的客户机上,两次 QueryPerformanceCounter 相差的数值会特别大。还不知道如何解决。。
测试程序如下
#include <windows.h>
DWORD WINAPI TestThread(PVOID x)
{
DWORD i = 0;
LARGE_INTEGER t0, t1, freq;
ULONG u0, u1;
ULONG tick0, tick1;
QueryPerformanceFrequency(&freq);
while(1)
{
tick0 = GetTickCount();
QueryPerformanceCounter(&t0);
Sleep(20);
tick1 = GetTickCount();
QueryPerformanceCounter(&t1);
u0 = (ULONG)(t0.QuadPart * 1000 / freq.QuadPart);
u1 = (ULONG)(t1.QuadPart * 1000 / freq.QuadPart);
if(u1 - u0 > 1000)
{
printf("tick0:%u u0:%u t0:%lld\n", tick0, u0, t0.QuadPart);
printf("tick1:%u u1:%u t1:%lld\n", tick1, u1, t1.QuadPart);
break;
}
}
return 0;
}
int main(int argc, char *argv[])
{
DWORD t;
CreateThread(NULL, 0, TestThread, 0, 0, &t);
while(1)
{
Sleep(1000);
}
return 0;
}
运行一段时间,就会有诡异的结果,如下
tick0:256061203 u0:484451150 t0:1896171838229
tick1:263509156 u1:499347347 t1:1954476477852
tick0:86261453 u0:86261495 t0:862614951505
tick1:93717562 u1:93717612 t1:937176121575
为什么Sleep(20)前后的tick相差巨大呢?
测试环境是 ESXi 6.7
客户机1 Win10 1703
客户机2 Win10 1803