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
27
28#include "delegate/IThread.h"
29#include "./predef/util/Timer.h"
30#include "ThreadMsg.h"
31#include <thread>
32#include <queue>
33#include <atomic>
34#include <condition_variable>
35#include <future>
36#include <optional>
37
38// Comparator for priority queue
40 bool operator()(const std::shared_ptr<ThreadMsg>& a, const std::shared_ptr<ThreadMsg>& b) const {
41 return static_cast<int>(a->GetPriority()) < static_cast<int>(b->GetPriority());
42 }
43};
44
48class Thread : public dmq::IThread
49{
50public:
55 Thread(const std::string& threadName, size_t maxQueueSize = 0);
56
59
64 bool CreateThread(std::optional<dmq::Duration> watchdogTimeout = std::nullopt);
65
67 void ExitThread();
68
70 std::thread::id GetThreadId();
71
73 static std::thread::id GetCurrentThreadId();
74
76 std::string GetThreadName() { return THREAD_NAME; }
77
79 size_t GetQueueSize();
80
84 virtual void DispatchDelegate(std::shared_ptr<dmq::DelegateMsg> msg);
85
86private:
87 Thread(const Thread&) = delete;
88 Thread& operator=(const Thread&) = delete;
89
91 void Process();
92
93 void SetThreadName(std::thread::native_handle_type handle, const std::string& name);
94
99 void WatchdogCheck();
100
105 void ThreadCheck();
106
107 std::unique_ptr<std::thread> m_thread;
108 std::priority_queue<std::shared_ptr<ThreadMsg>,
109 std::vector<std::shared_ptr<ThreadMsg>>,
110 ThreadMsgComparator> m_queue;
111 std::mutex m_mutex;
112 std::condition_variable m_cv;
113
114 // Condition variable to wake up blocked producers when space is available
115 std::condition_variable m_cvNotFull;
116
117 const std::string THREAD_NAME;
118
119 // Max queue size for back pressure (0 = unlimited)
120 const size_t MAX_QUEUE_SIZE;
121
122 // Promise and future to synchronize thread start
123 std::promise<void> m_threadStartPromise;
124 std::future<void> m_threadStartFuture;
125
126 std::atomic<bool> m_exit;
127
128 // Watchdog related members
129 std::atomic<dmq::TimePoint> m_lastAliveTime;
130 std::unique_ptr<Timer> m_watchdogTimer;
131 dmq::ScopedConnection m_watchdogTimerConn;
132 std::unique_ptr<Timer> m_threadTimer;
133 dmq::ScopedConnection m_threadTimerConn;
134 std::atomic<dmq::Duration> m_watchdogTimeout;
135};
136
137#endif
Interface for cross-thread delegate dispatching.
Cross-platform thread for any system supporting C++11 std::thread (e.g. Windows, Linux).
Definition cmsis-rtos2/Thread.h:33
std::string GetThreadName()
Get thread name.
Definition stdlib/Thread.h:76
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 cmsis-rtos2/Thread.cpp:42
Thread(const std::string &threadName, size_t maxQueueSize=0)
size_t GetQueueSize()
Get size of thread message queue.
Definition stdlib/Thread.cpp:99
void ExitThread()
Called once at program exit to shut down the worker thread.
A base class for a delegate enabled execution thread. Implemented by application code if asynchronous...
Definition IThread.h:22
RAII wrapper for Connection. Automatically disconnects when it goes out of scope.
Definition Signal.h:89
Definition stdlib/Thread.h:39
bool operator()(const std::shared_ptr< ThreadMsg > &a, const std::shared_ptr< ThreadMsg > &b) const
Definition stdlib/Thread.h:40