libmoost
moost::utils::fixed_interval_timer Class Reference

A fixed interval timer. More...

#include <fixed_interval_timer.hpp>

List of all members.

Classes

class  signal_t
 Represents an atomic signal. More...

Public Types

typedef boost::function0< void > callback_t
typedef std::pair
< boost::posix_time::ptime,
callback_t
process_arg
typedef boost::unique_future< int > future_t
 This future is set when the notification interval has passed.

Public Member Functions

 fixed_interval_timer (boost::posix_time::time_duration const &interval)
 Construct a new fixed interval timer.
 ~fixed_interval_timer ()
 Stop work and re-join the worker thread.
void notify (callback_t const &cb)
 Register a callback for notification.
void notify (future_t &f_sig)
 Register a future for notification.
void notify (signal_t &sig)
 Register a signal for notificatino.

Private Types

typedef boost::promise< int > promise_t
typedef boost::shared_ptr
< promise_t
promise_ptr
typedef std::sig_atomic_t volatile sig_atomic_t
typedef boost::shared_ptr
< sig_atomic_t
signal_ptr

Private Member Functions

void process (process_arg const &arg) const
void fulfil_promise (promise_ptr p_sig) const
void set_signal (signal_ptr psig) const

Private Attributes

boost::posix_time::time_duration interval_
boost::asio::io_service io_service_
boost::thread thread_
boost::shared_ptr
< boost::asio::io_service::work > 
pwork_

Detailed Description

A fixed interval timer.

Notifies either via a callback function or future once a fixed interval of time has passed. The frame of reference is taken from the time the notification request is made.

If the notification is to be via a callback function this function shall be called once the interval period has been surpassed.

If the notification is to be via a future the future shall be set once the notification period has been surpassed. It should be noted that the actual value of the set future is undefined. Once the notification period has passed a called to "is_ready" or "has_value" shall return true.

Since all notifications have to wait the same fixed period it should be clear the the sequence of notification is non-anachronistici; they will always trigger in the order they were registered. This property means we can use a FIFO queue to store and process the notification requests.

The astute reader will notice there may be a problem with this model. As the callback is fired in the same thread as that processing the queue it is imperative that each callback is lightweight and should not do any thing other than trip a signal or invoke a seperate asynchronous task. Failure to follow this criterian will lead to notification backlog.

The reason d'entre of this timer is to provide a simple and low cost way to generate fixed interval notifications. If you need something more complex than than this is not the timer for you and you should consider using a different timer (maybe the boost::asio::deadline_timer).

Definition at line 82 of file fixed_interval_timer.hpp.


Member Typedef Documentation

typedef boost::function0<void> moost::utils::fixed_interval_timer::callback_t

Definition at line 91 of file fixed_interval_timer.hpp.

typedef boost::unique_future<int> moost::utils::fixed_interval_timer::future_t

This future is set when the notification interval has passed.

Definition at line 144 of file fixed_interval_timer.hpp.

typedef std::pair<boost::posix_time::ptime, callback_t> moost::utils::fixed_interval_timer::process_arg

Definition at line 92 of file fixed_interval_timer.hpp.

typedef boost::shared_ptr<promise_t> moost::utils::fixed_interval_timer::promise_ptr [private]

Definition at line 86 of file fixed_interval_timer.hpp.

typedef boost::promise<int> moost::utils::fixed_interval_timer::promise_t [private]

Definition at line 85 of file fixed_interval_timer.hpp.

typedef std::sig_atomic_t volatile moost::utils::fixed_interval_timer::sig_atomic_t [private]

Definition at line 87 of file fixed_interval_timer.hpp.

typedef boost::shared_ptr<sig_atomic_t> moost::utils::fixed_interval_timer::signal_ptr [private]

Definition at line 88 of file fixed_interval_timer.hpp.


Constructor & Destructor Documentation

moost::utils::fixed_interval_timer::fixed_interval_timer ( boost::posix_time::time_duration const &  interval) [inline]

Construct a new fixed interval timer.

Parameters:
interval: interval between queuing and calling a callback

Definition at line 100 of file fixed_interval_timer.hpp.

Stop work and re-join the worker thread.

Definition at line 110 of file fixed_interval_timer.hpp.


Member Function Documentation

void moost::utils::fixed_interval_timer::fulfil_promise ( promise_ptr  p_sig) const [inline, private]

Definition at line 222 of file fixed_interval_timer.hpp.

Here is the caller graph for this function:

Register a callback for notification.

Parameters:
cb: this will be called when the interval passes

Definition at line 129 of file fixed_interval_timer.hpp.

Here is the call graph for this function:

Here is the caller graph for this function:

Register a future for notification.

Parameters:
f_sig: this is set when the interval passes

Definition at line 151 of file fixed_interval_timer.hpp.

Here is the call graph for this function:

Register a signal for notificatino.

Parameters:
sig: this is set when the interval passes

Definition at line 195 of file fixed_interval_timer.hpp.

Here is the call graph for this function:

void moost::utils::fixed_interval_timer::process ( process_arg const &  arg) const [inline, private]

Definition at line 212 of file fixed_interval_timer.hpp.

Here is the caller graph for this function:

void moost::utils::fixed_interval_timer::set_signal ( signal_ptr  psig) const [inline, private]

Definition at line 229 of file fixed_interval_timer.hpp.

Here is the caller graph for this function:


Member Data Documentation

boost::posix_time::time_duration moost::utils::fixed_interval_timer::interval_ [private]

Definition at line 235 of file fixed_interval_timer.hpp.

boost::asio::io_service moost::utils::fixed_interval_timer::io_service_ [private]

Definition at line 236 of file fixed_interval_timer.hpp.

boost::shared_ptr<boost::asio::io_service::work> moost::utils::fixed_interval_timer::pwork_ [private]

Definition at line 238 of file fixed_interval_timer.hpp.

Definition at line 237 of file fixed_interval_timer.hpp.


The documentation for this class was generated from the following file: