I need to add timers support in an application based on I/O Completion Ports (IOCP). I would like to avoid the use of a specific thread to manage timers.
On Linux, you can create a timer that delivers expiration notifications via a file descriptor (see timerfd.h man), so it's great to use it for example with epoll if your application is based on epoll.
On Windows, you can use "waitable timers" with an asynchronous procedure call (ACP) (see http://msdn.microsoft.com/en-us/library/ms686898(v=VS.85).aspx)
If you are interested, kqueue (BSD, Mac OS) supports timers by default (see EVFILT_TIMER).
With I/O Completion Ports, we have to use objets that support overlapped I/O. So, is there such a timer for IOCP ?
Best regards,
Cédrics
As far as I know there are no timers that generate a IOCP completion when they expire.
You could try the Windows timer queue; CreateTimerQueueTimer.
I ended up writing my own timer queue which does use an extra thread to run the timers, so it's probably no good for you: See here for a series of articles where I implement the queue with TDD and full unit tests. I'm in the process of implementing a higher performance TimerWheel with the same interface, but again that will use an external thread to manage the timers.
You could use waitable timers and queue a custom packet to the completion port using "PostQueuedCompletionStatus". But remember that if there are multiple worker threads only one of the thread will be notified.
来源:https://stackoverflow.com/questions/3239080/timer-that-supports-overlapped-i-o-for-iocp