DelegateMQ
Loading...
Searching...
No Matches
Thread Class Reference

Cross-platform thread for any system supporting C++11 std::thread (e.g. Windows, Linux). More...

#include <Thread.h>

Inheritance diagram for Thread:
dmq::IThread dmq::IThread

Public Member Functions

 Thread (const std::string &threadName)
 Constructor.
 
 ~Thread ()
 Destructor.
 
bool CreateThread ()
 
TaskHandle_t GetThreadId ()
 Get the ID of this thread instance.
 
std::string GetThreadName ()
 Get thread name.
 
virtual void DispatchDelegate (std::shared_ptr< dmq::DelegateMsg > msg)
 
 Thread (const std::string &threadName)
 Constructor.
 
 ~Thread ()
 Destructor.
 
bool CreateThread (std::optional< dmq::Duration > watchdogTimeout=std::nullopt)
 
void ExitThread ()
 Called once at program exit to shut down the worker thread.
 
std::thread::id GetThreadId ()
 Get the ID of this thread instance.
 
std::string GetThreadName ()
 Get thread name.
 
size_t GetQueueSize ()
 Get size of thread message queue.
 
virtual void DispatchDelegate (std::shared_ptr< dmq::DelegateMsg > msg)
 
- Public Member Functions inherited from dmq::IThread
virtual ~IThread ()=default
 Destructor.
 

Static Public Member Functions

static TaskHandle_t GetCurrentThreadId ()
 Get the ID of the currently executing thread.
 
static std::thread::id GetCurrentThreadId ()
 Get the ID of the currently executing thread.
 

Detailed Description

Cross-platform thread for any system supporting C++11 std::thread (e.g. Windows, Linux).

The Thread class creates a worker thread capable of dispatching and invoking asynchronous delegates.

// Create thread with a watchdog timeout thread->CreateThread(std::chrono::milliseconds(2000));

// Or create without a watchdog thread->CreateThread();

WatchdogCheck() is invoked by a Timer instance. In a real-time operating system (RTOS), Timer::ProcessTimers() is typically called from the highest-priority task in the system. This ensures that any user thread becoming unresponsive can still be detected, since WatchdogCheck() runs at a higher priority. For mission-critical systems, a hardware watchdog should also be used as a fail-safe.

Constructor & Destructor Documentation

◆ Thread() [1/2]

Thread::Thread ( const std::string & threadName)

Constructor.

◆ ~Thread() [1/2]

Thread::~Thread ( )

Destructor.

◆ Thread() [2/2]

Thread::Thread ( const std::string & threadName)

Constructor.

◆ ~Thread() [2/2]

Thread::~Thread ( )

Destructor.

Member Function Documentation

◆ CreateThread() [1/2]

bool Thread::CreateThread ( )

Called once to create the worker thread

Returns
TRUE if thread is created. FALSE otherise.

◆ CreateThread() [2/2]

bool Thread::CreateThread ( std::optional< dmq::Duration > watchdogTimeout = std::nullopt)

Called once to create the worker thread. If watchdogTimeout value provided, the maximum watchdog interval is used. Otherwise no watchdog.

Parameters
[in]watchdogTimeout- optional watchdog timeout.
Returns
TRUE if thread is created. FALSE otherise.

◆ DispatchDelegate() [1/2]

virtual void Thread::DispatchDelegate ( std::shared_ptr< dmq::DelegateMsg > msg)
virtual

Dispatch a DelegateMsg onto this thread. The implementer is responsible for getting the DelegateMsg into an OS message queue. Once DelegateMsg is on the destination thread of control, the IDelegateInvoker::Invoke() function must be called to execute the target function.

Parameters
[in]msgA shared pointer to the message.
Postcondition
The destination thread calls IThreadInvoker::Invoke() when DelegateMsg is received.

Implements dmq::IThread.

◆ DispatchDelegate() [2/2]

void Thread::DispatchDelegate ( std::shared_ptr< dmq::DelegateMsg > msg)
virtual

Dispatch and invoke a delegate target on the destination thread.

Parameters
[in]msg- Delegate message containing target function arguments.

Implements dmq::IThread.

◆ ExitThread()

void Thread::ExitThread ( )

Called once at program exit to shut down the worker thread.

◆ GetCurrentThreadId() [1/2]

std::thread::id Thread::GetCurrentThreadId ( )
static

Get the ID of the currently executing thread.

◆ GetCurrentThreadId() [2/2]

static std::thread::id Thread::GetCurrentThreadId ( )
static

Get the ID of the currently executing thread.

◆ GetQueueSize()

size_t Thread::GetQueueSize ( )

Get size of thread message queue.

◆ GetThreadId() [1/2]

std::thread::id Thread::GetThreadId ( )

Get the ID of this thread instance.

◆ GetThreadId() [2/2]

std::thread::id Thread::GetThreadId ( )

Get the ID of this thread instance.

◆ GetThreadName() [1/2]

std::string Thread::GetThreadName ( )
inline

Get thread name.

◆ GetThreadName() [2/2]

std::string Thread::GetThreadName ( )
inline

Get thread name.


The documentation for this class was generated from the following files: