1

I there any method to sleep the thread upto 100.8564 millisecond under window OS. I am using multimedia timer but its resolution is minimum 1 second. Kindly guide me so that I can handle the fractional part of the millisecond.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Siddiqui
  • 7,662
  • 17
  • 81
  • 129
  • Dupe of http://stackoverflow.com/questions/2927448/sleep-function-error-in-c by same user. –  May 31 '10 at 09:45
  • @Neil: If you read it, the issue is not duplicate to the one you are pointing to. The issue is to have a sleep with a decimal number as duration of sleep. – Kangkan May 31 '10 at 09:48
  • 3
    Why so precise? Surely 100.8565 milliseconds would be fine. – Puppy May 31 '10 at 09:48
  • @Arman: Do you really need that accurate a sleep timing? Can't you relook the need? – Kangkan May 31 '10 at 09:50
  • Actually I don't want to loss the resolution not even the fractional part of millisecond. 100.8564 is just of the demonstration of my question. – Siddiqui May 31 '10 at 09:50
  • I don't feel there will be some out of the box solution for such a precise timing on Windows. – Kangkan May 31 '10 at 09:59
  • 2
    @Arman, please tell us what are you building, and why you need such a timer in user mode windows. The thing is, that most people that ever asked me for such a timer, found ways around it. – Pavel Radzivilovsky May 31 '10 at 09:59
  • @Pavel, actually I am developing real time application, and there is no chance to ignore even the fractional part of millisecond, its cause very damage at the end of output. – Siddiqui May 31 '10 at 10:05
  • 3
    @Arman: Windows is not a realtime OS. You can't demand that the application is even executing in 100.8564 milliseconds from now. `Sleep()` tells Windows to not wake up the thread for *at least* the specified number of milliseconds. It might be longer before the thread is woken up. If you need more precise control, don't put the thread to sleep. You'll have to do active waiting then. But even then, Windows might be processing an interrupt or running another thread in 100.8564 milliseconds. You either need to run on a realtime OS or relax your requirements. – jalf May 31 '10 at 16:55
  • 1
    @Neil: how is that a duplicate? It is the same user, yes, and it is likely related, but the question looks very different to me. – jalf May 31 '10 at 16:56

4 Answers4

5

Yes you can do it. See QueryPerformanceCounter() to read accurate time, and make a busy loop.

This will enable you to make waits with up to 10 nanosecond resolution, however, if thread scheduler decides to steal control from you at the moment of the cycle end, it will, and there's nothing you can do about it except assigning your process realtime priority.

You may also have a look at this: http://msdn.microsoft.com/en-us/library/ms838340(WinEmbedded.5).aspx

Several frameworks were developed to do hard realtime on windows.

Otherwise, your question probably implies that you might be doing something wrong. There're numerous mechanisms to trick around ever needing precise delays, such as using proper bus drivers (in case of hardware/IO, or respective DMAs if you are designing a driver), and more.

Please tell us what exactly are you building.

Pavel Radzivilovsky
  • 18,794
  • 5
  • 57
  • 67
  • Actually I am developing real time application, and there is no chance to ignore even the fractional part of millisecond, its cause very damage at the end of output. – Siddiqui May 31 '10 at 10:27
  • 2
    @Arman This much I have guessed :) what application? What is it supposed to to? Why output timing is so important? Is it important Every Time, or an error in certain percentage of time is OKay? Because if not, what I wrote might depend on other things in the system, including UI, some UI processes (like taskman) can have RP, kernel mode code can interfere, etc. What EXACTLY is your mission? – Pavel Radzivilovsky May 31 '10 at 11:22
  • If sub-millisecond jitter can cause physical damage, then I would strongly suggest that you use dedicated hardware (based on an MCU or FPGA) to control that output. You could also incorporate a watchdog timer and other safety interlocks into that output control board. Use the Windows machine for higher-level control only. Or go with a real-time OS on a platform where you have access to a high-resolution timer and can write your own interrupt service routine. – Emile Cormier May 31 '10 at 20:14
  • You could also use some of of timer I/O card like the ones offered by National Instruments. National Instruments have drivers and C APIs that let you program their I/O cards easily. This would probably cost you the least in terms of R&D time. – Emile Cormier May 31 '10 at 20:19
3

I do not know your use case, but even a high end realtime operating system would be hard pressed to achieve less 100ns jitter on timings.

In most cases I found you do not need that precision in reproducibility but only for long time drift. In that respect it is relatively straightforward to keep a timeline and calculate the event on the desired precision. Then use that timeline to synchronize the events which may be off even by 10's of ms. As long as these errors do not add up, I found I got adequate performance.

Peter Tillemans
  • 34,983
  • 11
  • 83
  • 114
3

If you need guaranteed latency, you cannot get it with MS Windows. It's not a realtime operating system. It might swap in another thread or process at an importune instant. You might get a cache miss. When I did a robot controller a while back, I used an OS called On Time RTOS 32. It has an MS Windows API emulation layer. You can use it with Visual Studio. You'll need something like that.

Jive Dadson
  • 16,680
  • 9
  • 52
  • 65
2

The resolution of a multimedia timer is much better than one second. It can go down to 1 millisecond when you call timeBeginPeriod(1) first. The timer will automatically adjust its interval for the next call when the callback is delivered late. Which is inevitable on a multi-tasking operating system, there is always some kind of kernel thread with a higher priority than yours that will delay the callback.

While it will work pretty well on average, worst case latency is in the order of hundreds of milliseconds. Clearly, your requirements cannot be met by Windows by a long shot. You'll need some kind of microcontroller to supply that kind of execution guarantee.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536