Reputation: 30058
A long time ago I had a bug in my program. The root cause was that the C function
sleep(60);
would on rare occasions sleep less than 60 seconds. Or the function did cause the thread to sleep more than 60 s, but the clock was changed automatically by the OS (this seems likely since bug was happening only on XX::00::00
), aka it was manifesting itself rarely, and only on "round hour" (sleep shoudl have ended at >xh0m0s, it ended on x-1h59m59.99*s
).
Then my project manager went on a rant how he said million times that we should only use timers, not sleep.
From that time I accepted the notion that timers are more accurate than sleep(), but now I feel that I should ask for some more authoritative source.
So :
BTW OS was Linux, but I care about general answer if possible.
Upvotes: 5
Views: 4033
Reputation: 11499
Timers are definitely more accurate than sleep. Sleep is meant as just a rough measure of how long until the task scheduler revives a thread or process. Changes to the system clock, an overloaded task scheduler etc. will affect how long sleep actually sleeps for.
A timer will measure time more accurately. Generally speaking a timer will measure time accurately. There are two kinds of timers - ones based on the system clock like the functions in "time.h". Those will be affected by stuff like changes to the system clock. For example - if you change the system time, switch from daylight savings time, or suspend the machine etc. the actual measured time may be different from the real time.
The other kind of clocks are high resolution timers that are based on CPU ticks. These are timers like QueryPerformanceTimer on windows, and clock_gettime() on linux. These simply count cpu cycles. They won't be affected by changes to the system timer - but they will deviate from real world time in two ways:
What you want to do is sleep for a much shorter amount of time and use the clock that has the appropriate resolution. Eg. if you need to sleep for less than a few minutes, you should use high resolution timers. Sleep 100x more often than you need to and check elapsed time every time sleep comes back to see if the right amount of time has elapsed. If you need to sleep for more than a few minutes do the same but with the functons in time.h to check elapsed time.
If you need to be 100% accurate with time you may need specialized hardware- or to check real time periodically against an online time server - like the navy's atomic clock. ( http://tycho.usno.navy.mil/ntp.html)
Upvotes: 4
Reputation: 33126
There is no general answer for the simple reason that there is nothing in either the C or C++ standard that provides the ability to put an application to sleep. So the discussion is inherently going to be OS-dependent.
The unix sleep()
function has a coarse granularity. There's also usleep()
and nanosleep()
which have much finer granularity. The function select()
can also be used to put an application to sleep. Simply specify a timeout and no file descriptors.
Note #1: The interaction between sleep()
, usleep()
, nanosleep()
, itimers, and alarms is unspecified.
Note #2: Don't expect any of these mechanisms to have the precision of an atomic clock.
Upvotes: 2