DESERT 4.0.0
StoppableThread Class Referenceabstract

#include <stoppable_thread.h>

Inheritance diagram for StoppableThread:
Inheritance graph
Collaboration diagram for StoppableThread:
Collaboration graph

Public Member Functions

 StoppableThread ()=default
 
virtual ~StoppableThread ()=default
 
bool Start (bool exc_info=false)
 
void Stop (bool wait=false)
 
virtual void Run ()=0
 
template<class Rep , class Period >
void Sleep (const std::chrono::duration< Rep, Period > &d)
 
bool Running ()
 
bool StopRequested ()
 

Private Member Functions

void RunInternal ()
 

Private Attributes

std::atomic_bool m_running {false}
 
std::atomic_bool m_stop {false}
 
std::thread m_thread
 

Detailed Description

A stoppable C++11 thread implementation.

Constructor & Destructor Documentation

◆ StoppableThread()

StoppableThread::StoppableThread ( )
default

◆ ~StoppableThread()

virtual StoppableThread::~StoppableThread ( )
virtualdefault

Member Function Documentation

◆ Run()

virtual void StoppableThread::Run ( )
pure virtual

Pure virtual stopable worker function of thread, use the test StopRequested() to check if it should stop. Implementation example (use in derived class):

void Run() override { while (!StopRequested()) { Sleep(100ms); } }

Implemented in PositionListener< Owner >, and PositionListener< UDPPosition >.

Referenced by RunInternal().

◆ RunInternal()

void StoppableThread::RunInternal ( )
inlineprivate

Internal thread function.

References m_running, and Run().

Referenced by Start().

Here is the call graph for this function:

◆ Running()

bool StoppableThread::Running ( )
inline

Returns the current state of the thread.

Returns
true if running

References m_running.

Referenced by UDPPosition::command(), and Start().

◆ Sleep()

template<class Rep , class Period >
void StoppableThread::Sleep ( const std::chrono::duration< Rep, Period > &  d)
inline

Sleep for the given duration, use literals like 1s, 100ms, 10us

◆ Start()

bool StoppableThread::Start ( bool  exc_info = false)
inline

Start the thread.

Parameters
exc_infoprints a catched exception message to stderr
Returns
true if thread was started, false if not

References m_thread, RunInternal(), and Running().

Referenced by UDPPosition::command().

Here is the call graph for this function:

◆ Stop()

void StoppableThread::Stop ( bool  wait = false)
inline

Stop the thread, needs call(s) to StopRequested() in the Run() worker function to check for the stop request.

Parameters
waitwait for the thread to end

References m_stop, and m_thread.

Referenced by UDPPosition::command().

◆ StopRequested()

bool StoppableThread::StopRequested ( )
inline

Returns if a stop was requested.

Returns
true if a stop was requested

References m_stop.

Referenced by PositionListener< Owner >::Run().

Member Data Documentation

◆ m_running

std::atomic_bool StoppableThread::m_running {false}
private

Referenced by RunInternal(), and Running().

◆ m_stop

std::atomic_bool StoppableThread::m_stop {false}
private

Referenced by Stop(), and StopRequested().

◆ m_thread

std::thread StoppableThread::m_thread
private

Referenced by Start(), and Stop().


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