16 #include <condition_variable> 19 BEGIN_NAMESPACE_THREADING
20 enum class TimerMode : int
32 using CallbackFunction = std::function<void(void)>;
41 TimerThread(
int intervalMs = 1000, CallbackFunction callback =
nullptr,
int delayMs = -1, TimerMode timerMode = TimerMode::FixedDelay);
50 TimerThread(std::chrono::microseconds interval, CallbackFunction callback =
nullptr, std::optional<std::chrono::microseconds> delay = std::nullopt, TimerMode timerMode = TimerMode::FixedDelay);
57 void start()
override;
144 virtual void executeTimerCallback();
147 std::mutex terminateMutex;
148 std::condition_variable doTerminate;
149 std::atomic<int64_t> noOfCallbacks{0};
150 std::atomic<std::chrono::microseconds> intervalMcs;
151 std::atomic<std::chrono::microseconds> delayMcs;
153 CallbackFunction callback;
156 END_NAMESPACE_THREADING
void setCallback(const CallbackFunction &value)
Set callback function.
void stop() override
Stops thread.
ThreadEx class for working with threads.
Definition: threadex.h:46
Provides a mechanism for executing a method on a thread pool thread at specified intervals.
Definition: timerthread.h:29
void setTimerMode(TimerMode timerMode)
Set timer mode.
void setIntervalMs(const int value)
Set the amount of time in milliseconds.
void execute() override
Provides an abstract or pure virtual method to contain the code which executes when the thread is run...
int64_t getNoOfCallbacks() const
Function returns how many times Execute function was called.
int getIntervalMcs() const
Get the amount of time in microseconds.
int getDelayMcs() const
Get start delay (amount of time until first execution).
void setIntervalMcs(const int value)
Set the amount of time in microseconds.
void setDelayMcs(const int value)
Set start delay (the amount of time until first execution).
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...
ThreadEx is an abstract class that enables creation of separate threads of execution in an applicatio...
TimerMode getTimerMode() const
Get timer mode.
void start() override
Starts thread.
int getDelayMs() const
Get start delay (amount of time until first execution).
int getIntervalMs() const
Get the amount of time in milliseconds.
void setDelayMs(const int value)
Set start delay (the amount of time until first execution).
CallbackFunction & getCallback()
Get callback function.