9dan
9dan

Reputation: 4272

How do I execute 10ms CPU time operation exactly 30ms interval in Windows?

How do I execute a operation requiring around 10ms CPU time exactly 30ms interval in Windows? (I could allow +/- 5ms margin)

I tried and already failed with Sleep API :)

For next, I thought MM Timer API. But this blog post says it can't be done.
Why are the Multimedia Timer APIs (timeSetEvent) not as accurate as I would expect?

Also, MSDN says timeSetEvent is obsolete and we must use CreateTimerQueueTimer API. http://msdn.microsoft.com/en-us/library/dd757634(v=vs.85).aspx

Can CreateTimerQueueTimer be the solution?

Or do I need to use REALTIME_PRIORITY_CLASS process (I'm feeling sick)?

Upvotes: 2

Views: 396

Answers (1)

Ben Voigt
Ben Voigt

Reputation: 283614

I like CreateWaitableTimer for this. You can wait for it on a dedicated worker thread using WaitForSingleObject, a shared worker using WaitForMultipleObjects, or even on your UI thread by writing your main message loop to use MsgWaitForMultipleObjects.

Your 5ms precision should be achievable unless your system is badly overloaded. Thread priorities can help, but I/O is rarely prioritized, so if you do a blocking read of a file, you can be queued behind read requests from the same disk from much lower priority threads. Try to load any data in advance or use overlapped requests and deal with the condition that the request hasn't completed yet when your timer interval expires.

And I definitely would suggest using timeBeginPeriod to make all system timers more precise.

Upvotes: 4

Related Questions