commonlib
TimerThread Class Reference

Provides a mechanism for executing a method on a thread pool thread at specified intervals. More...

#include <timerthread.h>

Inheritance diagram for TimerThread:
ThreadEx

Public Types

using CallbackFunction = std::function< void(void)>
 

Public Member Functions

 TimerThread (int intervalMs=1000, CallbackFunction callback=nullptr, int delayMs=-1, TimerMode timerMode=TimerMode::FixedDelay)
 Initializes a new instance of the TimerThread class with default 1000 milliseconds period. More...
 
 TimerThread (std::chrono::microseconds interval, CallbackFunction callback=nullptr, std::optional< std::chrono::microseconds > delay=std::nullopt, TimerMode timerMode=TimerMode::FixedDelay)
 Initializes a new instance of the TimerThread class. More...
 
void start () override
 Starts thread.
 
void stop () override
 Stops thread.
 
int getIntervalMs () const
 Get the amount of time in milliseconds. More...
 
int getIntervalMcs () const
 Get the amount of time in microseconds. More...
 
void setIntervalMs (const int value)
 Set the amount of time in milliseconds. More...
 
void setIntervalMcs (const int value)
 Set the amount of time in microseconds. More...
 
int getDelayMs () const
 Get start delay (amount of time until first execution). More...
 
int getDelayMcs () const
 Get start delay (amount of time until first execution). More...
 
void setDelayMs (const int value)
 Set start delay (the amount of time until first execution). More...
 
void setDelayMcs (const int value)
 Set start delay (the amount of time until first execution). More...
 
void setTimerMode (TimerMode timerMode)
 Set timer mode. More...
 
TimerMode getTimerMode () const
 Get timer mode. More...
 
CallbackFunction & getCallback ()
 Get callback function. More...
 
void setCallback (const CallbackFunction &value)
 Set callback function. More...
 
int64_t getNoOfCallbacks () const
 Function returns how many times Execute function was called. More...
 
- Public Member Functions inherited from ThreadEx
void terminate ()
 Terminate sets the thread's Terminated property to true, signaling that the thread should be terminated as soon as possible.
 
void waitFor ()
 Waits for the thread to terminate.
 
const ThreadExPriority & getPriority () const
 Get thread's scheduling priority relative to other threads in the process. More...
 
void setPriority (ThreadExPriority priority)
 Set thread's scheduling priority relative to other threads in the process. More...
 
std::recursive_mutex & getLock ()
 Get thread's locking object. More...
 
const std::atomic< bool > & getTerminated () const
 Returns thread's terminated signal. More...
 
const std::atomic< bool > & getFinished () const
 Returns true if thread has already been finished. More...
 
bool getStarted () const
 Returns true if thread is started. More...
 
ThreadExceptionAdapter * getExceptionAdapter ()
 Returns exception information holder. More...
 
void setExceptionAdapter (ThreadExceptionAdapter *exceptionAdapter)
 Sets exception information holder. More...
 
const std::thread::id getThreadId () const
 Returns thread id.
 

Protected Member Functions

void execute () override
 Provides an abstract or pure virtual method to contain the code which executes when the thread is run.
 
virtual void executeTimerCallback ()
 
- Protected Member Functions inherited from ThreadEx
void setThreadName (const char *name)
 Set current thread name (you must call it from the thread)
 

Additional Inherited Members

- Protected Attributes inherited from ThreadEx
std::recursive_mutex lock
 
std::atomic< bool > terminated
 
std::atomic< bool > finished
 
ThreadExceptionAdapter * exceptionAdapter {}
 

Detailed Description

Provides a mechanism for executing a method on a thread pool thread at specified intervals.

Constructor & Destructor Documentation

◆ TimerThread() [1/2]

TimerThread::TimerThread ( int  intervalMs = 1000,
CallbackFunction  callback = nullptr,
int  delayMs = -1,
TimerMode  timerMode = TimerMode::FixedDelay 
)

Initializes a new instance of the TimerThread class with default 1000 milliseconds period.

Parameters
intervalMsa interval in milliseconds.
callbacka callback function you want the TimerThread to execute.
delayMsstart delay (amount of time before first execution) in milliseconds. If negative, then the interval time is used for start delay.
timerModea timer mode.

◆ TimerThread() [2/2]

TimerThread::TimerThread ( std::chrono::microseconds  interval,
CallbackFunction  callback = nullptr,
std::optional< std::chrono::microseconds >  delay = std::nullopt,
TimerMode  timerMode = TimerMode::FixedDelay 
)

Initializes a new instance of the TimerThread class.

Parameters
intervala interval in microseconds.
callbacka callback function you want the TimerThread to execute.
delaystart delay (amount of time before first execution) in microseconds. If empty, then the interval time is used for start delay.
timerModea timer mode.

Member Function Documentation

◆ getCallback()

CallbackFunction& TimerThread::getCallback ( )

Get callback function.

Returns
Callback function.

◆ getDelayMcs()

int TimerThread::getDelayMcs ( ) const

Get start delay (amount of time until first execution).

Returns
Delay time in microseconds.

◆ getDelayMs()

int TimerThread::getDelayMs ( ) const

Get start delay (amount of time until first execution).

Returns
Delay time in milliseconds.

◆ getIntervalMcs()

int TimerThread::getIntervalMcs ( ) const

Get the amount of time in microseconds.

Returns
Interval time in microseconds.

◆ getIntervalMs()

int TimerThread::getIntervalMs ( ) const

Get the amount of time in milliseconds.

Returns
Interval time in milliseconds.

◆ getNoOfCallbacks()

int64_t TimerThread::getNoOfCallbacks ( ) const

Function returns how many times Execute function was called.

Returns
number of calls

◆ getTimerMode()

TimerMode TimerThread::getTimerMode ( ) const

Get timer mode.

Default timer mode is TimerMode::FixedDelay.

Returns
Timer mode

◆ setCallback()

void TimerThread::setCallback ( const CallbackFunction &  value)

Set callback function.

Parameters
valueA function that will be called every interval.

◆ setDelayMcs()

void TimerThread::setDelayMcs ( const int  value)

Set start delay (the amount of time until first execution).

Parameters
valuea interval in microseconds.

◆ setDelayMs()

void TimerThread::setDelayMs ( const int  value)

Set start delay (the amount of time until first execution).

Parameters
valuea interval in milliseconds.

◆ setIntervalMcs()

void TimerThread::setIntervalMcs ( const int  value)

Set the amount of time in microseconds.

Parameters
valuea interval in microseconds.

◆ setIntervalMs()

void TimerThread::setIntervalMs ( const int  value)

Set the amount of time in milliseconds.

Parameters
valuea interval in milliseconds.

◆ setTimerMode()

void TimerThread::setTimerMode ( TimerMode  timerMode)

Set timer mode.

Parameters
timerModetimer mode

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