Search code examples
epoll

Why does epoll_wait only provide huge 1ms timeout?


epoll_wait, select and poll functions all provide a timeout. However with epoll, it's at a large resolution of 1ms. Select & ppoll are the only one providing sub-millisecond timeout.

That would mean doing other things at 1ms intervals at best. I could do a lot of other things within 1ms on a modern CPU.

So to do other things more often than 1ms I actually have to provide a timeout of zero (essentially disabling it). And I'd probably add my own usleep somewhere in the main loop to stop it chewing up too much CPU.

So the question is, why is the timeout in milli's when I would think clearly there is a case for a higher resolution timeout.


Solution

  • Since you are on Linux, instead of providing a zero timeout value and manually usleeeping in the loop body, you could simply use the timerfd API. This essentially lets you create a timer (with a resolution finer than 1ms) associated with a file descriptor, which you can add to the set of monitored descriptors.