event loop epoll timer out of sync issue

30 Views Asked by At

I am writing an event loop. I want to have timers. I will implement the timers using epoll timeout. I will have a timing wheel and will mod the current time by wheel size to get the slot in the wheel. But I think there may be an issue with this. Let's say the current time is 1.99999999999, and I check the wheel and the next event is in 4 seconds. Since the next event is in 4 seconds, I sleep for 4 seconds. However since the time was just on the edge of becoming 2, when I wake from the 4 seconds, the time will be 6 seconds. Therefore I will have missed my event which was meant to trigger in 4 seconds since that should happen at time 5 seconds.

I could just scrap the timing wheel and use a binary heap. That way I could just check if the time of the event is less than or equal to the current time. But this comes at the cost of performance.

1

There are 1 best solutions below

0
Simon Walke On

Agree with the comments earlier: not really enough detail. I would second the comment from @erik258 about timer_create() and I would further add that for Linux-specific integration of epoll and timers, take a look at the man pages for timerfd_create() as well.