DESERT 3.5.1
Loading...
Searching...
No Matches
StoppableThread Class Referenceabstract

#include <stoppable_thread.h>

Inheritance diagram for StoppableThread:

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.

Definition at line 49 of file stoppable_thread.h.

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 >.

◆ RunInternal()

void StoppableThread::RunInternal ( )
inlineprivate

Internal thread function.

Definition at line 125 of file stoppable_thread.h.

◆ Running()

bool StoppableThread::Running ( )
inline

Returns the current state of the thread.

Returns
true if running

Definition at line 117 of file stoppable_thread.h.

◆ 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.

Definition at line 110 of file stoppable_thread.h.

◆ 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

Definition at line 58 of file stoppable_thread.h.

◆ 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

Definition at line 82 of file stoppable_thread.h.

◆ StopRequested()

bool StoppableThread::StopRequested ( )
inline

Returns if a stop was requested.

Returns
true if a stop was requested

Definition at line 121 of file stoppable_thread.h.

Member Data Documentation

◆ m_running

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

Definition at line 131 of file stoppable_thread.h.

◆ m_stop

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

Definition at line 132 of file stoppable_thread.h.

◆ m_thread

std::thread StoppableThread::m_thread
private

Definition at line 133 of file stoppable_thread.h.


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