Windows C/Sleep() 函数在时钟漂移期间将如何运行?
如果我运行 Sleep(10000) 之类的命令,并且系统时钟在此期间发生变化,线程是否仍会休眠 10 秒的挂钟时间,或者更少或更多?即 Sleep() 函数是否将毫秒转换为硬件滴答声?
If I run something like Sleep(10000), and the system clock changes during that, will the thread still sleep for 10 seconds of wall-clock time, or less or more? I.e. does the Sleep() function convert milliseconds into hardware ticks?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Sleep() 与挂钟时间无关。它基于用于线程调度的同一计时器。
不过,由于系统时钟的频率以及超时后您实际被安排运行的时间,您大多数情况下不会睡正好 10 秒。
Sleep() is independent of the wall-clock time. It is based on the same timer that is used for thread scheduling.
You will mostly not sleep exactly 10 seconds though, due to the frequency of the system clock and when you actually get scheduled to run after the timeout elapses.
据我所知,它没有在任何地方记录下来,但是,是的,你可以放心地假设是这样。如果不这样做的话,那将是相当具有破坏性的,因为日光转换每年发生两次。 记录了等效的内核函数 KeDelayExecutionThread 的 Interval 参数:
指定等待发生的绝对或相对时间(以 100 纳秒为单位)。负值表示相对时间。绝对过期时间跟踪系统时间的任何变化;相对过期时间不受系统时间变化的影响。
It isn't documented anywhere that I know of, but yes, you can safely assume so. It would be quite disruptive if it didn't, daylight transitions happen twice a year. It is documented for the equivalent kernel function, KeDelayExecutionThread's Interval argument:
Specifies the absolute or relative time, in units of 100 nanoseconds, for which the wait is to occur. A negative value indicates relative time. Absolute expiration times track any changes in system time; relative expiration times are not affected by system time changes.