Timers have better accuracy than sleep ()

For a long time I had a mistake in my program. The main reason was that the function C

sleep(60);

in rare cases, will sleep less than 60 seconds. Or the function really made the thread sleep for more than 60 s, but the clock was automatically changed by the OS (this seems probable because the error only happened on XX::00::00), otherwise it appeared very rarely, and only on the β€œround” hour (sleep shoudl ended at> xh0m0s, it ended on x-1h59m59.99*s).
Then my project manager continued to say, as he said millions of times, that we should use only timers and not sleep. Since then I agreed that timers are more accurate than sleep (), but now I I feel that I should ask for a more authoritative source. :

  • are timers more accurate than sleep?
  • (related) are they located deeply (at the OS level), implemented using different methods?
    I know that timers are used for callbacks, sleep just delays the execution of the current thread, I'm talking about the execution of a delay in the implementation.

BTW OS was Linux, but if possible, I need a generic answer.

+5
source share
2 answers

Timers are certainly more accurate than sleep. Sleep is understood as an approximate indicator of how long a task scheduler revitalizes a thread or process. Changes in the system clock, overloaded task scheduler, etc. Affect how long really sleep.

. , . - , "time.h". , . , , .. .

- , . , QueryPerformanceTimer, clock_gettime() linux. . - :

  • , , , .
  • , CPU , .

, , - , . . , . 100 , , , , , . , , functons time.h, .

100% , , - - , - . (http://tycho.usno.navy.mil/ntp.html)

+3

, C ++ , . , OS-.

unix sleep() . usleep() nanosleep(), . select() . - .

# 1: sleep(), usleep(), nanosleep(), .

# 2: , - .

+2

All Articles