Reputation: 915
I understand that the Sleep()
function in windows can only ensure that your thread sleeps for at least the specified time. While Sleep()
cannot ensure that the thread only sleeps more than the specified time, would it be possible to improve the accuracy of the thread's sleep time by using SetPriority() to increase the priority of the process/thread?
Upvotes: 0
Views: 2272
Reputation:
The MSDN article says the following:
If dwMilliseconds is greater than one tick but less than two, the wait can be anywhere between one and two ticks, and so on. To increase the accuracy of the sleep interval, call the timeGetDevCaps function to determine the supported minimum timer resolution and the timeBeginPeriod function to set the timer resolution to its minimum. Use caution when calling timeBeginPeriod, as frequent calls can significantly affect the system clock, system power usage, and the scheduler. If you call timeBeginPeriod, call it one time early in the application and be sure to call the timeEndPeriod function at the very end of the application.
So priority does not affect sleep from what I can read, but it's possible another application can (cause) delay (between) thread switching so your application will be less acurate anyway.
You can try using high resolution timers to try making your own nanosecond-precision sleep.
Upvotes: 1