WinAPI Sleep()函数调用的休眠时间比预期的长 [英] WinAPI Sleep() function call sleeps for longer than expected

查看:1206
本文介绍了WinAPI Sleep()函数调用的休眠时间比预期的长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

操作系统:Windows 7

OS: Windows 7

当调用WinAPI Sleep()函数作为Sleep(1)时,线程实际上睡眠15ms。我在一个循环中做了100次,总的睡眠时间是1500ms而不是100次。

When calling the WinAPI Sleep() function as Sleep(1) the thread actually sleeps for 15ms. I did it 100 times in a loop and the total sleep time was 1500ms instead of 100.

这是常见的行为,或者我应该协调我的MOBO ,CPU,Windows安装?

Is this common behavior or should I be concerced about something being wrong with my MOBO, CPU, Windows installion?

编辑:如果可能,你可以运行这段代码,并发布睡眠时间。我让我的一个朋友运行这个,他实际上有一切都在1ms。

If possible could you run this code and post how long the sleep time was. I let a friend of mine run this, and he actually had it all at 1ms.

#include <iostream>
#include <ctime>
#include <Windows.h>

void test(void)
{
    std::cout << "Testing 1ms sleep." << std::endl;

    for (unsigned int i = 0; i < 10; i++)
    {
        std::clock_t startClocks = std::clock();

        Sleep(1);

        std::clock_t clocksTaken = std::clock() - startClocks;
        std::cout << "Time: " << clocksTaken << "ms." << std::endl;
    }
}

int main(void)
{
    test();

    std::cin.sync();
    std::cin.get();
    return 0;
}

EDIT2:似乎一些人得到1ms的原因是其他程序正在运行,将系统范围的定时器分辨率设置为1ms。

It seems that the reason why some people are getting 1ms is that some other program is running that sets the system-wide timer resolution to 1ms. By default this should be 15.6ms on Windows 7.

推荐答案


这是常见的行为

Is this common behavior

它是。

窗口的线程调度器工作在一个时间量的各种因素,包括Windows版本和版本)。任何非零延迟有效地四舍五入到一个完整的量程。

Window's thread scheduler works on a time quantum (exact length depends of various factors including Windows version and edition). Effectively any non-zero delay is rounded up to a complete quantum.

这篇关于WinAPI Sleep()函数调用的休眠时间比预期的长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆