DelegateMQ
Loading...
Searching...
No Matches
Timer Class Reference

A thread-safe timer class that provides periodic or one-shot callbacks. More...

#include <Timer.h>

Public Member Functions

 Timer (void)
 Constructor.
 
 ~Timer (void)
 Destructor.
 
void Start (dmq::Duration timeout, bool once=false)
 
void Stop ()
 Stops a timer.
 
bool Enabled ()
 

Static Public Member Functions

static dmq::TimePoint GetNow ()
 
static void ProcessTimers ()
 

Public Attributes

dmq::SignalPtr< void(void)> OnExpired
 Client's register with OnExpired to get timer callbacks.
 

Detailed Description

A thread-safe timer class that provides periodic or one-shot callbacks.

The Timer class allows clients to register for callbacks (OnExpired) that are invoked when a specified timeout interval elapses.

Key Features:

  • Thread Safe: All public API methods (Start, Stop, etc.) are thread-safe and can be called from any thread.
  • Flexible Modes: Supports both one-shot (once = true) and periodic (once = false) operation.
  • Deterministic Execution: Callbacks are invoked on the thread that calls ProcessTimers(). This allows the user to control exactly which thread executes the timer logic (e.g., Main Thread, GUI Thread, or a dedicated Worker Thread).

Usage Note: This class uses a cooperative polling model. The static method Timer::ProcessTimers() MUST be called periodically (e.g., inside a main loop or a dedicated thread loop) to service active timers and dispatch callbacks.

See also
SafeTimer.cpp for examples on how to handle callbacks safely with object lifetimes.

Constructor & Destructor Documentation

◆ Timer()

Timer::Timer ( void )

Constructor.

◆ ~Timer()

Timer::~Timer ( void )

Destructor.

Member Function Documentation

◆ Enabled()

bool Timer::Enabled ( )
inline

Gets the enabled state of a timer.

Returns
TRUE if the timer is enabled, FALSE otherwise.

◆ GetNow()

dmq::TimePoint Timer::GetNow ( )
static

Get the time.

Returns
The time now.

◆ ProcessTimers()

void Timer::ProcessTimers ( )
static

Called on a periodic basic to service all timer instances. @TODO: Call periodically for timer expiration handling.

◆ Start()

void Timer::Start ( dmq::Duration timeout,
bool once = false )

Starts a timer for callbacks on the specified timeout interval.

Parameters
[in]timeout- the timeout.
[in]once- true if only one timer expiration

◆ Stop()

void Timer::Stop ( )

Stops a timer.

Member Data Documentation

◆ OnExpired

dmq::SignalPtr<void(void)> Timer::OnExpired

Client's register with OnExpired to get timer callbacks.


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