"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > ## Why Does Sleep(1) in WinAPI Cause a Lengthier Slumber Than Expected?

## Why Does Sleep(1) in WinAPI Cause a Lengthier Slumber Than Expected?

Published on 2024-10-31
Browse:476

## Why Does Sleep(1) in WinAPI Cause a Lengthier Slumber Than Expected?

WinAPI Sleep Function Conundrum: Lengthier Slumber Than Expected

When invoking the WinAPI Sleep function with a parameter of 1, users may encounter thread delays exceeding the intended duration. In fact, a repeated series of 100 Sleep(1) calls has yielded a total sleep time of 1500ms, much greater than the stipulated 100ms.

Cause of the Extended Sleep:

This behavior is inherent in the Windows thread scheduler. It operates with a time quantum, or a defined period during which a thread can execute. Non-zero delays are inherently rounded up to the nearest complete quantum.

Plausible System Issues:

While the observed behavior is typical, it can be advantageous to rule out potential system issues that might exacerbate the problem:

  • Motherboard (MOBO): Faulty MOBO hardware can impact clock accuracy, contributing to longer sleep periods.
  • CPU: Similar to MOBO issues, CPU irregularities can also lead to timing discrepancies.
  • Windows Installation: System errors or incomplete installations can disrupt the timekeeping capabilities of Windows.

Additional Considerations:

It's noteworthy that other running programs can adjust the system-wide timer resolution, potentially resulting in a smaller quantum size. This can explain instances where Sleep(1) delays are reported to be within the intended 1ms timeframe. The default timer resolution in Windows 7 should be around 15.6ms, not 1ms.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3