Efficient timer algorithm

What is the best algorithm for implementing a simple timer library. The library must allow the following:

  • Triggered timers
  • Timers to Stop
  • Timers to be checked whether they will be executed

Enabling the timer by the timer will call the callback function.

The timer module will allow timers to have Ns time resolution, and each Ns must be assigned to the module in order to cause the module to check expired timers.

Many timers can be simultaneously active.

A better algorithm should meet the following goals

  • Be reliable to start / stop timers when handling a timer expiration callback
  • Allow start, stop and check timers quickly
  • We have a small amount of memory

Hi

+7
algorithm timer
source share
3 answers

The best algorithm I've seen for timers is the timer wheel found in the research article Hash and Hierarchical Chrome Wheels: Data Structures for Effectively Implementing the Facility Timer

I know that Java has an implementation with Netty, JBoss, and I'm sure of a different place that you can use if you write in Java.

+8
source share

Timers are usually best implemented in the kernel of the operating system at the assembly / C level, using platform features such as APIC timers.

You can look at http://lwn.net/Articles/167897/ for details on the Linux implementation and break through the Linux source code to see working implementations.

+2
source share

On POSIX-ish systems, you can use the timer_create / timer_settime family of functions to provide a lot of this β€œfor free.”

+1
source share

All Articles