开发者

Event loop in C++ on Windows with timer queue

I am writing a windows DLL in C++ and I need some sort of event loop. Several events are periodically checked and the DLL then triggers callbacks in the client appl开发者_运维问答ication. The polling period might differ for different events.

I wonder what is the best way to code this. The timing precision must be good but performance is the main priority. Is it better to have several (4-5) timers in one Windows timer queue with various periods or one single timer with the smallest period? Or should I prefer a completely different solution?

Thanks


What sort of performance is the main priority? Repeatable, accurate timing is err... 'highly difficult' on a general-purpose OS like Windows because of the numerous driver interrupts. There are various timer mechanisms that can generate very accurate timeouts but not reliably. How much jitter can you tolerate? In what sort of range are your intervals? Do the callbacks have to be in any particular thread context, or could anything fire the events?

Rgds, Martin


To directly answer the question you can set up a timer with SetTimer which takes a nIDEvent which is usually registered to a window handle. That window will then receive WM_TIMER messages with the registered identifier as wParam. You should destroy the timer when you're done with it with KillTimer. WM_TIMER has a granularity of around 10-15ms on Windows NT systems.

As said by the other posters, you would probably be better off with a truly event based system. Perhaps you could edit your post with the usecase and better answers could be suggested.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜