DelegateMQ
Loading...
Searching...
No Matches
stdlib/Thread.h
Go to the documentation of this file.
1#ifndef _THREAD_STD_H
2#define _THREAD_STD_H
3
4// @see https://github.com/endurodave/StdWorkerThread
5// David Lafreniere, Feb 2017.
6
7#include "delegate/IThread.h"
9#include "ThreadMsg.h"
10#include <thread>
11#include <queue>
12#include <mutex>
13#include <atomic>
14#include <condition_variable>
15#include <future>
16#include <optional>
17
18// Comparator for priority queue
20 bool operator()(const std::shared_ptr<ThreadMsg>& a, const std::shared_ptr<ThreadMsg>& b) const {
21 return static_cast<int>(a->GetPriority()) < static_cast<int>(b->GetPriority());
22 }
23};
24
40class Thread : public dmq::IThread
41{
42public:
44 Thread(const std::string& threadName);
45
48
53 bool CreateThread(std::optional<dmq::Duration> watchdogTimeout = std::nullopt);
54
56 void ExitThread();
57
59 std::thread::id GetThreadId();
60
62 static std::thread::id GetCurrentThreadId();
63
65 std::string GetThreadName() { return THREAD_NAME; }
66
68 size_t GetQueueSize();
69
73 virtual void DispatchDelegate(std::shared_ptr<dmq::DelegateMsg> msg);
74
75private:
76 Thread(const Thread&) = delete;
77 Thread& operator=(const Thread&) = delete;
78
80 void Process();
81
82 void SetThreadName(std::thread::native_handle_type handle, const std::string& name);
83
88 void WatchdogCheck();
89
94 void ThreadCheck();
95
96 std::unique_ptr<std::thread> m_thread;
97 std::priority_queue<std::shared_ptr<ThreadMsg>,
98 std::vector<std::shared_ptr<ThreadMsg>>,
99 ThreadMsgComparator> m_queue;
100 std::mutex m_mutex;
101 std::condition_variable m_cv;
102 const std::string THREAD_NAME;
103
104 // Promise and future to synchronize thread start
105 std::promise<void> m_threadStartPromise;
106 std::future<void> m_threadStartFuture;
107
108 std::atomic<bool> m_exit;
109
110 // Watchdog related members
111 std::atomic<dmq::Duration> m_lastAliveTime;
112 std::unique_ptr<Timer> m_watchdogTimer;
113 std::unique_ptr<Timer> m_threadTimer;
114 std::atomic<dmq::Duration> m_watchdogTimeout;
115};
116
117#endif
118
A base class for a delegate enabled execution thread. Implemented by application code if asynchronous...
Cross-platform thread for any system supporting C++11 std::thread (e.g. Windows, Linux).
Definition freertos/Thread.h:24
std::string GetThreadName()
Get thread name.
Definition stdlib/Thread.h:65
Thread(const std::string &threadName)
Constructor.
virtual void DispatchDelegate(std::shared_ptr< dmq::DelegateMsg > msg)
std::thread::id GetThreadId()
Get the ID of this thread instance.
static std::thread::id GetCurrentThreadId()
Get the ID of the currently executing thread.
~Thread()
Destructor.
bool CreateThread()
Definition freertos/Thread.cpp:28
size_t GetQueueSize()
Get size of thread message queue.
Definition stdlib/Thread.cpp:95
void ExitThread()
Called once at program exit to shut down the worker thread.
Definition stdlib/Thread.cpp:120
Definition IThread.h:17
Definition stdlib/Thread.h:19
bool operator()(const std::shared_ptr< ThreadMsg > &a, const std::shared_ptr< ThreadMsg > &b) const
Definition stdlib/Thread.h:20