+ 2
Why are Thread.Sleep(), time.sleep(), this_thread::sleep_for(chrono::milliseconds()), etc. inaccurate?
Why are these sleep functions inaccurate?
3 Answers
+ 8
Thread.Sleep(n) means block the current thread for at least the number of timeslices (or thread quantums) that can occur within n milliseconds. The length of a timeslice is different on different versions/types of Windows and different processors and generally ranges from 15 to 30 milliseconds. This means the thread is almost guaranteed to block for more than n milliseconds. The likelihood that your thread will re-awaken exactly after n milliseconds is about as impossible as impossible can be. So, Thread.Sleep is pointless for timing.
Source:
http://blogs.msmvps.com/peterritchie/2007/04/26/thread-sleep-is-a-sign-of-a-poorly-designed-program/
+ 3
use select() syscall
+ 1
Thank you Ali!