Multi callback timer under Linux and c/c++

Posted on 2004-03-25
Last Modified: 2007-12-19
I am looking for an way to handle in c++ and linux timers like in java (timer.schedule) or in Win32/MFC (SetTimer) with a callback after elapsed time. What about support of multiple callbacks with different times???

The SIG_ALRM/alarm does only support 1 callback with bad time resolution of 1 second. But I need multiple callbacks with short intervalls like that:

Timer t1 = new Timer (func1, 1000);
Timer t2 = new Timer (func2, 60);

Can anybody give me a tip??? I know how to implement with multiple threads and sleep but is there no implementation out there???


Question by:zuse01
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 45

Expert Comment

ID: 10684629
Hi zuse01,

There is no direct way (API) to do what you want ... If sleep solution is OK with you, the you can use usleep or nanosleep, but be warned that even with nanosleep, best resolution that you can expect is 10ms ... You can use sleep with pthreads to achieve what you want ...

An alternative is presented in linux kernel in the form of timer queue, though programming it might not be convenient ... dont kow if newer kernel versions still support it ...


Author Comment

ID: 10684649
hi sunnycoder!

You mean
not convenient to program an interface like in java with phread and sleep
LVL 45

Expert Comment

ID: 10684695

I meant not convenient to program in the kernel using timer queues

pthread and usleep will be lot more easy to use


Accepted Solution

oumer earned 400 total points
ID: 10685804
Since there is a penality of upto 10ms when using sleep commands in linux, if your timing constraint is very tight, you should avoid using them.

The way I will do it (actually that is how i do it on my current development project): Have a scheduler that mainatins a list of priority queue of timerhandlers, and process this priority queue forever inside the main thread of the scheduler. when a timer needs firing, call the callback method of your timer ...

I am assuming you are using c++, and the code is  not complete, and the protections may have to be changed (private, public ,...)  according to your need, here I have put all public for simplicity, if you need details, please let me know,

I have a timer handler base class

class TimerHandler
  struct timeval expiry;
  double periodic_expiry; //if you have periodic timer, set this to the period, otherwise it should be zero
  void setExpirationTime(struct timeval);
  void setExpirationTime(double);
  virtual void callback()=0;

Depending on your need you can subclass from this TimerHandler. Or if your timers are very similar, just make =0 from the callback to make it non pure virtual and define it in your class.

And you have some scheduler class that will have a member of priority queue to hold timers that should fire ...

struct TimerHandlerComparision   //used in the priority queue to compare timers
  bool operator()(TimerHandler *x, TimerHandler *y)
    struct timeval tx=x->getExpirationTime(), ty=y->getExpirationTime();
    return (((tx.tv_sec - ty.tv_sec)+ ((((double)(tx.tv_usec - ty.tv_usec)))/1000000) ) > 0);
    //return ((*x) > (*y));

class Scheduler
TimerHandlerComparision> timeline;
  bool Register(TimerHandler*);
  void processNextEvent(void);
  void run();

The run method should be the entry to the scheduler's thread. inside the main method you do something like


in the processnextevent()

if (this->timeline.empty())
      //do some i/o stuff, if you want or just leave it empty
      //we have to check if there is any timer(s) that needs to fire
      TimerHandler *timer = this->;
      gettimeofday(&temp, NULL);
      exp = timer->expiry;
      time_diff = (exp.tv_sec - temp.tv_sec)+ ((double)(exp.tv_usec - temp.tv_usec))/1000000;
      if (time_diff < = 0.0) // time has passed for the timer

In the register method, put the timer in the priority queue, with something like ..

//add current time value to the expiration time of the timer (which was relative when the timer was created to an absolute time ....)
struct timeval temp;
gettimeofday(&temp, NULL);
struct timeval temp_time = t->getExpirationTime();
 double temp_time_float =temp_time.tv_sec + (((double)temp_time.tv_usec)/1000000);
temp_time_float += (temp.tv_sec + (((double)temp.tv_usec)/1000000));

In the timerhandler's callback you can do whatever you want, for example spawn another thread to do some stuff

A typical code for the callback will be something like ....

for a non periodic timer

  do stuff()

for a periodic timer

   do stuff();
   this->setExpirationTime(periodic_expiry) //cause when we reach this expiry has become an absoulte value instead of realtive ...


Expert Comment

ID: 10689034

setitimer allow you to specify microsecond resolution.
as far as i can remeber you can only have one timer running at once, but you can easily handle this by doing something similar to that described above. Keep your timers in a queue and when a timer expires (you get SIAG_ALARM) run thru the list calling expire() on each object, then it will see if it has expired and do whatever takes your fancy.


Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor ( Top Charts is a view in which you can set seve…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question