gpt4 book ai didi

c++ - 提高 Windows Sleep() 的准确性

转载 作者:行者123 更新时间:2023-11-28 07:34:22 24 4
gpt4 key购买 nike

据我了解,Windows 中的Sleep() 函数只能确保您的线程至少 休眠指定的时间。而Sleep()不能确保线程只休眠超过指定时间,是否可以通过使用SetPriority()提高进程的优先级来提高线程休眠时间的准确性/线程?

最佳答案

MSDN article说如下:

If dwMilliseconds is greater than one tick but less than two, the wait can be anywhere between one and two ticks, and so on. To increase the accuracy of the sleep interval, call the timeGetDevCaps function to determine the supported minimum timer resolution and the timeBeginPeriod function to set the timer resolution to its minimum. Use caution when calling timeBeginPeriod, as frequent calls can significantly affect the system clock, system power usage, and the scheduler. If you call timeBeginPeriod, call it one time early in the application and be sure to call the timeEndPeriod function at the very end of the application.

所以根据我的阅读,优先级不会影响 sleep ,但是另一个应用程序可能会(导致)延迟(之间)线程切换,因此您的应用程序无论如何都会不太准确。

您可以尝试使用高分辨率计时器来实现您自己的纳秒精度 sleep 。

关于c++ - 提高 Windows Sleep() 的准确性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17031306/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com