views:

105

answers:

3

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?

+8  A: 

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.

Michael
If they are independent, then if at midnight (00:00:00) I do Sleep(10000), and five seconds later (00:00:05), I reset the system clock to midnight (00:00:00), does that mean that 15 seconds of wall-clock time will pass before the Sleep(10000) wakes up? Is that mechanism different from drift as implemented by the Windows Time Service?
Richard Franks
Sleep does not use WTS. It is based on the system clock ticks and tick frequency. This usage of the system clock shouldn't be confused with the clock on your wall.
sixlettervariables
A: 

The Sleep() function, in most cases, is based off of the system clock. If you call Sleep(10000) when your system clock is at time T, then your program will not wake up until (at the soonest) when your system clock reads time T+10000, regardless of how much time has passed on the wall clock.

VeeArr
+3  A: 

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.

Hans Passant