VMware QueryPerformanceCounter/GetTickCount 悬案

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 16:13   1408   0

最近发现在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

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP