• <tfoot id='2XIiW'></tfoot>

      <bdo id='2XIiW'></bdo><ul id='2XIiW'></ul>
    <legend id='2XIiW'><style id='2XIiW'><dir id='2XIiW'><q id='2XIiW'></q></dir></style></legend>

      <small id='2XIiW'></small><noframes id='2XIiW'>

      1. <i id='2XIiW'><tr id='2XIiW'><dt id='2XIiW'><q id='2XIiW'><span id='2XIiW'><b id='2XIiW'><form id='2XIiW'><ins id='2XIiW'></ins><ul id='2XIiW'></ul><sub id='2XIiW'></sub></form><legend id='2XIiW'></legend><bdo id='2XIiW'><pre id='2XIiW'><center id='2XIiW'></center></pre></bdo></b><th id='2XIiW'></th></span></q></dt></tr></i><div id='2XIiW'><tfoot id='2XIiW'></tfoot><dl id='2XIiW'><fieldset id='2XIiW'></fieldset></dl></div>

      2. WinAPI Sleep() 函数调用休眠的时间比预期的要长

        时间:2023-08-02

        <legend id='P0GHk'><style id='P0GHk'><dir id='P0GHk'><q id='P0GHk'></q></dir></style></legend>
        <tfoot id='P0GHk'></tfoot>
          <bdo id='P0GHk'></bdo><ul id='P0GHk'></ul>
            <tbody id='P0GHk'></tbody>
            <i id='P0GHk'><tr id='P0GHk'><dt id='P0GHk'><q id='P0GHk'><span id='P0GHk'><b id='P0GHk'><form id='P0GHk'><ins id='P0GHk'></ins><ul id='P0GHk'></ul><sub id='P0GHk'></sub></form><legend id='P0GHk'></legend><bdo id='P0GHk'><pre id='P0GHk'><center id='P0GHk'></center></pre></bdo></b><th id='P0GHk'></th></span></q></dt></tr></i><div id='P0GHk'><tfoot id='P0GHk'></tfoot><dl id='P0GHk'><fieldset id='P0GHk'></fieldset></dl></div>

              1. <small id='P0GHk'></small><noframes id='P0GHk'>

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

                  问题描述

                  操作系统:Windows 7

                  OS: Windows 7

                  当调用 WinAPI Sleep() 函数作为 Sleep(1) 时,线程实际上会休眠 15 毫秒.我循环了 100 次,总睡眠时间是 1500 毫秒而不是 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?

                  如果可能,您可以运行此代码并发布睡眠时间有多长.我让我的一个朋友运行了这个,他实际上在 1 毫秒内就完成了.

                  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;
                  }
                  

                  似乎有些人得到 1ms 的原因是其他一些程序正在运行,将系统范围的计时器分辨率设置为 1ms.默认情况下,这在 Windows 7 上应该是 15.6 毫秒.

                  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

                  是的.

                  Window 的线程调度程序在时间片上工作(确切长度取决于各种因素,包括 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() 函数调用休眠的时间比预期的要长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:我可以使用 CreateFile,但将句柄强制转换为 std: 下一篇:哪种字体是 MFC 对话框控件的默认字体?

                  相关文章

                  最新文章

                    1. <small id='Wbtjq'></small><noframes id='Wbtjq'>

                      <legend id='Wbtjq'><style id='Wbtjq'><dir id='Wbtjq'><q id='Wbtjq'></q></dir></style></legend><tfoot id='Wbtjq'></tfoot>

                        <bdo id='Wbtjq'></bdo><ul id='Wbtjq'></ul>
                    2. <i id='Wbtjq'><tr id='Wbtjq'><dt id='Wbtjq'><q id='Wbtjq'><span id='Wbtjq'><b id='Wbtjq'><form id='Wbtjq'><ins id='Wbtjq'></ins><ul id='Wbtjq'></ul><sub id='Wbtjq'></sub></form><legend id='Wbtjq'></legend><bdo id='Wbtjq'><pre id='Wbtjq'><center id='Wbtjq'></center></pre></bdo></b><th id='Wbtjq'></th></span></q></dt></tr></i><div id='Wbtjq'><tfoot id='Wbtjq'></tfoot><dl id='Wbtjq'><fieldset id='Wbtjq'></fieldset></dl></div>