Sleep and Windows XP

Started by
7 comments, last by RP 18 years, 7 months ago
Hi, I have an application with several threads running. When I call Sleep(1) in a given thread, the thread will sleep between 10-15 miliseconds instead of 1. I heard this is specific to Windows XP. How can I get my thread to sleep only 1 (or x) miliseconds. Is there maybe a possibility to set the resolution of thread-swapping lower? (change some operating system variable?) Thanks a lot
Advertisement
The resolution of the Sleep function will vary from an absolute minimal possible of 10ms to anything above based on hardware and bios. There is nothing you can do about it to the best of my knowledge. Quick read here.
You can't. There is no guarantee on the upper bound on the amount of time until your thread is woken. If you need that, you'll have to investigate a real-time OS. You could play with the priority level of your thread a bit and it might help (don't starve the system!), but, in the end, there is still no definite bound.
i've found that the Sleep time resolution is governed by the multimedia timer. but i've never been able to go below 10ms.

TIMECAPS tc;timeGetDevCaps(&tc, sizeof(tc));...timeBeginPeriod(tc.wPeriodMin);Sleep(m_snooze);timeEndPeriod  (tc.wPeriodMin);DoWorkHere......

You can use the timeBeginPeriod(1) to request the fastest swapping the OS will allow. There's no good reason why a modern PC can't swap at 1000Hz deterministically, but it seems that the faster systems actually have higher limits. We used to get ~1.0ms swaps on 300MHz systems, but a 2.4Ghz swaps at 1.6ms.

The guarantee provided by the Win32 Sleep function is that the thread will sleep for at least as long as you request (which is actually more certainty than you get from posix).
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Quote:Original post by Shannon Barber


I'll have to definitly give that a try sometime - I've never seen that informaton before anywhere. Doing Google searches on timeBeginPeriod now gets a lot of great infomration on this issue, thanks for sharing! I guess there is something you can do about it. If only MSDN had better cross-referencing...
Thanks a lot to all of you for your useful information :)
By the way, you can also use the high performance counter: QueryPerformanceCounter (), QueryPerformanceFrequency ().
U can use multimedia timer. But thats no full guaranty.

This topic is closed to new replies.

Advertisement