DelegateMQ
Loading...
Searching...
No Matches
win32/Thread.h
Go to the documentation of this file.
1#ifndef _THREAD_WIN32_H
2#define _THREAD_WIN32_H
3
27
28#include "delegate/IThread.h"
29#include "./predef/util/Timer.h"
30#include "ThreadMsg.h"
31#include <queue>
32#include <atomic>
33#include <optional>
34#include <string>
35
36#ifdef _WIN32
37#ifndef NOMINMAX
38#define NOMINMAX
39#endif
40#include <Windows.h>
41#endif
42
43// Comparator for priority queue
45 bool operator()(const std::shared_ptr<ThreadMsg>& a, const std::shared_ptr<ThreadMsg>& b) const {
46 return static_cast<int>(a->GetPriority()) < static_cast<int>(b->GetPriority());
47 }
48};
49
53class Thread : public dmq::IThread
54{
55public:
60 Thread(const std::string& threadName, size_t maxQueueSize = 0);
61
63 virtual ~Thread();
64
69 bool CreateThread(std::optional<dmq::Duration> watchdogTimeout = std::nullopt);
70
72 void ExitThread();
73
75 DWORD GetThreadId();
76
78 static DWORD GetCurrentThreadId();
79
81 std::string GetThreadName() { return THREAD_NAME; }
82
84 size_t GetQueueSize();
85
89 virtual void DispatchDelegate(std::shared_ptr<dmq::DelegateMsg> msg) override;
90
91private:
92 Thread(const Thread&) = delete;
93 Thread& operator=(const Thread&) = delete;
94
96 static DWORD WINAPI ThreadProc(LPVOID lpParam);
97
99 void Process();
100
105 void WatchdogCheck();
106
111 void ThreadCheck();
112
113 HANDLE m_hThread = NULL;
114 DWORD m_threadId = 0;
115
116 // Manual-reset event to synchronize thread startup
117 HANDLE m_hStartEvent = NULL;
118
119 CRITICAL_SECTION m_cs;
120
121 // Condition variable to wake up consumers when a message is enqueued
122 CONDITION_VARIABLE m_cvNotEmpty;
123
124 // Condition variable to wake up blocked producers when space is available
125 CONDITION_VARIABLE m_cvNotFull;
126
127 std::priority_queue<std::shared_ptr<ThreadMsg>,
128 std::vector<std::shared_ptr<ThreadMsg>>,
129 ThreadMsgComparator> m_queue;
130
131 const std::string THREAD_NAME;
132
133 // Max queue size for back pressure (0 = unlimited)
134 const size_t MAX_QUEUE_SIZE;
135
136 std::atomic<bool> m_exit;
137
138 // Watchdog related members
139 std::atomic<dmq::TimePoint> m_lastAliveTime;
140 std::unique_ptr<Timer> m_watchdogTimer;
141 dmq::ScopedConnection m_watchdogTimerConn;
142 std::unique_ptr<Timer> m_threadTimer;
143 dmq::ScopedConnection m_threadTimerConn;
144 std::atomic<dmq::Duration> m_watchdogTimeout;
145};
146
147#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
virtual ~Thread()
Destructor.
std::string GetThreadName()
Get thread name.
Definition win32/Thread.h:81
virtual void DispatchDelegate(std::shared_ptr< dmq::DelegateMsg > msg) override
DWORD GetThreadId()
Get the ID of this thread instance.
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.
void ExitThread()
Called once at program exit to shut down the worker thread.
static DWORD GetCurrentThreadId()
Get the ID of the currently executing thread.
A base class for a delegate enabled execution thread. Implemented by application code if asynchronous...
Definition IThread.h:22
RAII handle to a single Signal subscription. Disconnects automatically on destruction....
Definition Signal.h:104
Definition stdlib/Thread.h:39
bool operator()(const std::shared_ptr< ThreadMsg > &a, const std::shared_ptr< ThreadMsg > &b) const
Definition win32/Thread.h:45