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"
8#include <thread>
9#include <queue>
10#include <mutex>
11#include <atomic>
12#include <condition_variable>
13#include <future>
14
15class ThreadMsg;
16
17class Thread : public dmq::IThread
18{
19public:
21 Thread(const std::string& threadName);
22
25
29
31 void ExitThread();
32
34 std::thread::id GetThreadId();
35
37 static std::thread::id GetCurrentThreadId();
38
40 std::string GetThreadName() { return THREAD_NAME; }
41
43 size_t GetQueueSize();
44
45 virtual void DispatchDelegate(std::shared_ptr<dmq::DelegateMsg> msg);
46
47private:
48 Thread(const Thread&) = delete;
49 Thread& operator=(const Thread&) = delete;
50
52 void Process();
53
54 void SetThreadName(std::thread::native_handle_type handle, const std::string& name);
55
56 std::unique_ptr<std::thread> m_thread;
57 std::queue<std::shared_ptr<ThreadMsg>> m_queue;
58 std::mutex m_mutex;
59 std::condition_variable m_cv;
60 const std::string THREAD_NAME;
61
62 // Promise and future to synchronize thread start
63 std::promise<void> m_threadStartPromise;
64 std::future<void> m_threadStartFuture;
65
66 std::atomic<bool> m_exit;
67};
68
69#endif
70
A base class for a delegate enabled execution thread. Implemented by application code if asynchronous...
Definition freertos/Thread.h:19
std::string GetThreadName()
Get thread name.
Definition stdlib/Thread.h:40
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()
size_t GetQueueSize()
Get size of thread message queue.
Definition stdlib/Thread.cpp:72
void ExitThread()
Called once a program exit to exit the worker thread.
Definition stdlib/Thread.cpp:97
A class to hold a platform-specific thread messsage that will be passed through the OS message queue.
Definition freertos/ThreadMsg.h:7
Definition IThread.h:17