views:

415

answers:

3

What is the best algorithm to implement a simple timer library. The library should allow the following:

  1. Timers to be started
  2. Timers to be stopped
  3. Timers to be checked whether they are still running

On Timer expiry a callback function will be called.

The timer module will allow timers to have a time resolution of Ns and the module shall be given a kick every Ns to prompt the module to check for expired timers.

Many timers may be simultaneously active.

The best algorithm needs to meet the following goals

  1. Be Robust to timers being started / stopped while processing a timer expiry callback
  2. Allow timers to be started, stopped and checked quickly
  3. Have a small memory footprint

Regards

+1  A: 

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

Kristopher Johnson
Hi Kristopher,I'll have a look at these but I am more interested in the algorithm than taking a stock library. Regards
Howard May
+1  A: 

Timers are typically best implemented in an operating system kernel, at the assembly/C level, making use of platform-specific features like APIC timers wherever possible.

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

kquinn
A: 

Is this post still alive? If yes, i have an efficient implementation of the same. Will post it here :)...so reply

Yes it is alive, if perhaps a little unwell.
Howard May