commonlib
timerthread.h
Go to the documentation of this file.
1 
10 #pragma once
11 
12 #include "threadex.h"
13 #include <atomic>
14 #include <mutex>
15 #include <functional>
16 #include <condition_variable>
17 #include <optional>
18 
19 BEGIN_NAMESPACE_THREADING
20 enum class TimerMode : int
21 {
22  FixedDelay = 0,
23  FixedRate = 1
24 };
25 
29 class TimerThread : public ThreadEx
30 {
31 public:
32  using CallbackFunction = std::function<void(void)>;
33 
41  TimerThread(int intervalMs = 1000, CallbackFunction callback = nullptr, int delayMs = -1, TimerMode timerMode = TimerMode::FixedDelay);
42 
50  TimerThread(std::chrono::microseconds interval, CallbackFunction callback = nullptr, std::optional<std::chrono::microseconds> delay = std::nullopt, TimerMode timerMode = TimerMode::FixedDelay);
51 
52  ~TimerThread() override;
53 
57  void start() override;
58 
62  void stop() override;
63 
68  int getIntervalMs() const;
69 
74  int getIntervalMcs() const;
75 
80  void setIntervalMs(const int value);
81 
86  void setIntervalMcs(const int value);
87 
92  int getDelayMs() const;
93 
98  int getDelayMcs() const;
99 
104  void setDelayMs(const int value);
105 
110  void setDelayMcs(const int value);
111 
116  void setTimerMode(TimerMode timerMode);
117 
122  TimerMode getTimerMode() const;
123 
128  CallbackFunction& getCallback();
129 
134  void setCallback(const CallbackFunction& value);
135 
140  int64_t getNoOfCallbacks() const;
141 
142 protected:
143  void execute() override;
144  virtual void executeTimerCallback();
145 
146 private:
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;
152  TimerMode timerMode;
153  CallbackFunction callback;
154 };
155 
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.