Could someone explain what the difference is between epoll
, poll
and threadpool?
- What are the pros / cons?
- Any suggestions for frameworks?
- Any suggestions for simple/basic tutorials?
- It seems that
epoll
andpoll
are Linux-specific... Is there an equivalent alternative for Windows?
Threadpool does not really fit into the same category as poll and epoll, so I will assume you are referring to threadpool as in "threadpool to handle many connections with one thread per connection".
Pros and cons
epoll
, though the obvious way (all threads block onepoll_wait
) is of no use, because epoll will wake up every thread waiting on it, so it will still have the same issues.futex
is your friend here, in combination with e.g. a fast forward queue per thread. Although badly documented and unwieldy,futex
offers exactly what's needed.epoll
may return several events at a time, andfutex
lets you efficiently and in a precisely controlled manner wake N blocked threads at a time (N beingmin(num_cpu, num_events)
ideally), and in the best case it does not involve an extra syscall/context switch at all.fork
(a.k.a. old fashion threadpool)fork
is not "free", although the overhead is mostly coalesced by the copy-on-write mechanism. On large datasets which are also modified, a considerable number of page faults followingfork
may negatively impact performance.poll
/select
epoll
epoll_ctl
)epoll_wait
)poll
workstimerfd
andeventfd
(stunning timer resolution and accuracy, too).signalfd
, eliminating the awkward handling of signals, making them part of the normal control flow in a very elegant manner.eventfd
, but requires a (to date) undocumented function.poll
may perform equally or better.epoll
cannot do "magic", i.e. it is still necessarily O(N) in respect to the number of events that occur.epoll
plays well with the newrecvmmsg
syscall, since it returns several readiness notifications at a time (as many as are available, up to whatever you specify asmaxevents
). This makes it possible to receive e.g. 15 EPOLLIN notifications with one syscall on a busy server, and read the corresponding 15 messages with a second syscall (a 93% reduction in syscalls!). Unluckily, all operations on onerecvmmsg
invokation refer to the same socket, so it is mostly useful for UDP based services (for TCP, there would have to be a kind ofrecvmmsmsg
syscall which also takes a socket descriptor per item!).EAGAIN
even when usingepoll
because there are exceptional situations whereepoll
reports readiness and a subsequent read (or write) will still block. This is also the case forpoll
/select
on some kernels (though it has presumably been fixed).EAGAIN
is returned upon receiving a notification, it is possible to indefinitely read new incoming data from a fast sender while completely starving a slow sender (as long as data keeps coming in fast enough, you might not seeEAGAIN
for quite a while!). Applies topoll
/select
in the same manner.The documentation states that several threads waiting on one epoll are all signalled. It further states that a notification tells you whether IO activity has happened since the last call to
epoll_wait
(or since the descriptor was opened, if there was no previous call).The true, observable behaviour in edge-triggered mode is much closer to "wakes the first thread that has called
epoll_wait
, signalling that IO activity has happened since anyone last called eitherepoll_wait
or a read/write function on the descriptor, and thereafter only reports readiness again to the next thread calling or already blocked inepoll_wait
, for any operations happening after anyone called a of read (or write) function on the descriptor". It kind of makes sense, too... it just isn't exactly what the documentation suggests.kqueue
epoll
, different usage, similar effect.Frameworks
libevent -- The 2.0 version also supports completion ports under Windows.
ASIO -- If you use Boost in your project, look no further: You already have this available as boost-asio.
Any suggestions for simple/basic tutorials?
The frameworks listed above come with extensive documentation. The Linux docs and MSDN explains epoll and completion ports extensively.
Mini-tutorial for using epoll:
Mini-tutorial for IO completion ports (note calling CreateIoCompletionPort twice with different parameters):
(These mini-tuts omit all kind of error checking, and hopefully I didn't make any typos, but they should for the most part be ok to give you some idea.)
EDIT:
Note that completion ports (Windows) conceptually work the other way around as epoll (or kqueue). They signal, as their name suggests, completion, not readiness. That is, you fire off an asynchronous request and forget about it until some time later you're told that it has completed (either successfully nor not so much successfully, and there is the exceptional case of "completed immediately" too).
With epoll, you block until you are notified that either "some data" (possibly as little as one byte) has arrived and is available or there is sufficient buffer space so you can do a write operation without blocking. Only then, you start the actual operation, which then will hopefully not block (other than you would expect, there is no strict guarantee for that -- it is therefore a good idea to set descriptors to nonblocking and check for EAGAIN [EAGAIN and EWOULDBLOCK for sockets, because oh joy, the standard allows for two different error values]).