Boost logo

Boost Users :

Subject: Re: [Boost-users] [asio] Interval Timer
From: Andrew Holden (aholden_at_[hidden])
Date: 2011-09-21 10:49:23


On Wednesday, September 21, 2011 8:15 AM, asif saeed wrote:
> I need an interval timer to continuously call my callback function
> after every n milliseconds/seconds for ever once I set it up. I
> also need the ability to kill/cancel the timer. Does Boost provide
> such a facility now?

You could assemble that timer using asio's deadline_timer

http://www.boost.org/doc/libs/1_47_0/doc/html/boost_asio/reference/deadl
ine_timer.html

The deadline_timer calls a callback at a specified time. Your callback
would need to calculate the time of the next callback and restart the
timer. Perhaps the following pseudocode could get you started:

Public data members:
* deadline_timer::duration_type trigger_interval
* bool is_killed

Private data members:
* deadline_timer::time_type next_trigger
* deadline_timer the_timer

Constructor:
is_killed = false
Set trigger_interval
next_trigger = now + trigger_interval
Initialize the_timer
Call the_timer.expires_at

Callback:
if (! is_killed and no errors happened)
{
  next_trigger += trigger_interval
  Call the_timer.expires_at
  Do your processing
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net