Is there anyway at all in the windows environment to sleep for ~1 microsecond? After researching and reading many threads and various websites, I have not been able to see that this is possible. Since the scheduler appears to be the limiting factor and it operates at the 1 millisecond level, then I believe it can't be done without going to a real time OS.
-
2Seems to me like you've answered your own question. I don't see why your choice of language really matters here. – Karl Knechtel Jul 20 '11 at 21:52
-
and what would be the use case? – Karoly Horvath Jul 20 '11 at 21:52
-
You could do some busy waiting. – Nobody moving away from SE Jul 20 '11 at 21:53
-
If you need to do something that require microsecond precision, you'll pretty much use a dedicated device for that (i.e. microcontroller or FGPA), not a general-purpose PC. – In silico Jul 20 '11 at 21:54
-
Have you looked at MMCSS to get more real-time like behavior from high priority threads? http://msdn.microsoft.com/en-us/library/ms684247%28v=VS.85%29.aspx – selbie Jul 21 '11 at 07:38
2 Answers
Despite the fact that windows is claimed to be not a "real-time" OS, events can be generated at microsecond resolution. The use of a combination of system time (file_time) and the performance counter frequency has been described at other places. However, careful implementation with taking care about processor affinity and process/thread priorities opens the door to timed events at microsecond resolution.
Since the windows scheduler and the windows timer services do rely on the systems interrupt mechanism, the microsecond can only be tuned for by polling. Particulary on multicore systems polling is not so ugly anymore. And the polling only has to last for the shortest possible interrupt period. The multimedia timer interface allows to put the interrupt period down to about 1ms, thus one can get near the desired (microsecond resolution) time and the polling will last for 1ms at most.
My implementation of microsecond resolution time services for windows, test code and an extensive description can be found at the Windows Timestamp Project located at windowstimestamp.com
-
1If you advertise a product/webpage you are affiliated with you should disclose your affiliation in your answer. – IInspectable Jan 04 '13 at 20:21
It may not be the most portable, and I've not used these functions myself, but it might be possible to use the information in the High-Resolution Timer section of this link and block: QueryPerformanceCounter

- 9,064
- 3
- 31
- 49
-
Thanks, I will look into the various suggestions put forth above. – Al Kurlansky Jul 21 '11 at 15:59