+ 2

Why are Thread.Sleep(), time.sleep(), this_thread::sleep_for(chrono::milliseconds()), etc. inaccurate?

Why are these sleep functions inaccurate?

21st Nov 2016, 2:35 PM
Samuel Neo
Samuel Neo - avatar
3 Respostas
+ 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/
21st Nov 2016, 3:34 PM
Alireza M
Alireza M - avatar
+ 3
use select() syscall
10th Jan 2017, 5:49 AM
Suvaditya
Suvaditya - avatar
+ 1
Thank you Ali!
22nd Nov 2016, 12:24 AM
Samuel Neo
Samuel Neo - avatar