Timers/timeouts in multi-threaded event-loops
The traditional way to integrate timeout handling (or timers) in (single-threaded) event loops was to just pass the appropriate timeout value to the select/poll/epoll syscall. While this works fine for single-threaded event loops it doesn't really work for multi-threaded event loops (unless you are happy to use a dedicated thread for timeout handling, etc).
So what's the alternative for the fully multi-threaded case? The most obvious option appears to be POSIX (realtime) timers (see timer_create). Although these were originally specified to use Unix signals for notifications, most modern Unix implementations provide an alternative notification mechanism that better integrates with a multi-threaded design. On Linux, there is a timerfd_create syscall to create a special timer file descriptor (which then integrates nicely with epoll) and FreeBSD/NetBSD provide special support for timers in kqueue/kevent.
For some work-in-progress code on how to implement this on Linux, head over to my nginetd project.
-- cmeerw.org

