Resource Tuner
Loading...
Searching...
No Matches
Timer Class Reference

Timer. More...

#include <Timer.h>

Public Member Functions

 Timer (std::function< void(void *)> callBack, int8_t isRecurring=false)
 Initialize the Timer.
 
int8_t startTimer (int64_t duration)
 Starts the timer for the given duration in milliseconds.
 
void killTimer ()
 Invalidates current timer.
 

Private Attributes

int64_t mDuration
 Duration of the timer.
 
int8_t mIsRecurring
 Flag to set a recurring timer. It is never modified. False by default.
 
std::atomic< int8_t > mTimerStop
 Flag to let the timer thread know it has been killed.
 
std::condition_variable mTimerCond
 Condition variable to stop the thread for the timer duration and wake up either after the duration has ended or the timer is killed.
 
std::function< void(void *)> mCallback
 Callback function to be called after timer is over.
 

Detailed Description

Timer.

Definition at line 20 of file Timer.h.

Constructor & Destructor Documentation

◆ Timer()

Timer::Timer ( std::function< void(void *)>  callBack,
int8_t  isRecurring = false 
)

Initialize the Timer.

Parameters
callBackFunction that needs to be invoked after the specified timer duration (in milliseconds) has expired.
isRecurringFlag to indicate if a timer is recurring (false by default). If set to true, the registered callback will be called after every time interval window, say 5 seconds. This flag is useful for daemon threads like Pulse Monitor.

Member Function Documentation

◆ startTimer()

int8_t Timer::startTimer ( int64_t  duration)

Starts the timer for the given duration in milliseconds.

As part of this routine, a task is submitted to the Thread Pool, this task forces the thread (which picked up the submitted task) to wait (on Condition Variable) for the given duration (wait_for). When woken up, the thread atomically checks if the timer was updated or not. Following which it it executes a pre-registered callback function.

Parameters
durationTime Interval (in milliseconds) after which the Callback needs needs to be triggered.
Returns
int8_t:
  • 1 if the timer was successfully started
  • 0 otherwise.

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