DelegateMQ
Loading...
Searching...
No Matches
qt/Thread.h
Go to the documentation of this file.
1#ifndef _QT_THREAD_H
2#define _QT_THREAD_H
3
6
7#include "delegate/IThread.h"
8#include <QThread>
9#include <QObject>
10#include <QMutex>
11#include <QWaitCondition>
12#include <memory>
13#include <string>
14
15// Ensure DelegateMsg is known to Qt MetaType system
16Q_DECLARE_METATYPE(std::shared_ptr<dmq::DelegateMsg>)
17
18class Worker;
19
20class Thread : public QObject, public dmq::IThread
21{
22 Q_OBJECT
23
24public:
27 Thread(const std::string& threadName);
28
31
34
36 void ExitThread();
37
39 QThread* GetThreadId();
40
42 static QThread* GetCurrentThreadId();
43
44 std::string GetThreadName() const { return m_threadName; }
45
46 // IThread Interface Implementation
47 virtual void DispatchDelegate(std::shared_ptr<dmq::DelegateMsg> msg) override;
48
49signals:
50 // Internal signal to bridge threads
51 void SignalDispatch(std::shared_ptr<dmq::DelegateMsg> msg);
52
53private:
54 Thread(const Thread&) = delete;
55 Thread& operator=(const Thread&) = delete;
56
57 std::string m_threadName;
58 QThread* m_thread = nullptr;
59 Worker* m_worker = nullptr;
60};
61
62// ----------------------------------------------------------------------------
63// Worker Object
64// Lives on the target QThread and executes the slots
65// ----------------------------------------------------------------------------
66class Worker : public QObject
67{
68 Q_OBJECT
69public slots:
70 void OnDispatch(std::shared_ptr<dmq::DelegateMsg> msg)
71 {
72 if (msg) {
73 auto invoker = msg->GetInvoker();
74 if (invoker) {
75 invoker->Invoke(msg);
76 }
77 }
78 }
79};
80
81#endif // _QT_THREAD_H
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
static QThread * GetCurrentThreadId()
Get the current executing QThread pointer.
~Thread()
Destructor.
virtual void DispatchDelegate(std::shared_ptr< dmq::DelegateMsg > msg) override
Enqueues a delegate message for execution on this thread.
QThread * GetThreadId()
Get the QThread pointer (used as the ID)
bool CreateThread()
Create and start the internal QThread.
Thread(const std::string &threadName, size_t maxQueueSize=0)
Definition cmsis-rtos2/Thread.cpp:16
void ExitThread()
Stop the QThread.
void SignalDispatch(std::shared_ptr< dmq::DelegateMsg > msg)
std::string GetThreadName() const
Definition qt/Thread.h:44
Definition qt/Thread.h:67
void OnDispatch(std::shared_ptr< dmq::DelegateMsg > msg)
Definition qt/Thread.h:70
A base class for a delegate enabled execution thread. Implemented by application code if asynchronous...
Definition IThread.h:22