Core Library  1.7.0.0
Library containing core utilities and tools for threading, networking, logging, INI and CSV file management etc.
core_lib::threads::ThreadRunner Class Referencefinal

ThreadRunner class. More...

#include <ThreadRunner.h>

Inheritance diagram for core_lib::threads::ThreadRunner:
core_lib::threads::ThreadBase

Public Member Functions

 ThreadRunner ()=delete
 Default constructor deleted.
 
 ThreadRunner (const ThreadRunner &)=delete
 Copy constructor deleted.
 
ThreadRunneroperator= (const ThreadRunner &)=delete
 Copy assignment operator deleted.
 
 ThreadRunner (ThreadRunner &&)=delete
 Copy constructor deleted.
 
ThreadRunneroperator= (ThreadRunner &&)=delete
 Copy assignment operator deleted.
 
 ThreadRunner (const thread_function_t &threadFunction, const thread_function_t &processTerminationConditions, bool autoStart=false)
 Initialisation constructor. More...
 
 ~ThreadRunner () override
 Destructor.
 
void SleepThreadForTime (unsigned int milliSecs) const
 Make this thread sleep for a period of time. More...
 
- Public Member Functions inherited from core_lib::threads::ThreadBase
 ThreadBase (const ThreadBase &)=delete
 Copy constructor deleted.
 
ThreadBaseoperator= (const ThreadBase &)=delete
 Copy assignment operator deleted.
 
 ThreadBase (ThreadBase &&)=default
 Move constructor deleted.
 
ThreadBaseoperator= (ThreadBase &&)=default
 Move assignment operator deleted.
 
virtual ~ThreadBase ()=default
 Destructor.
 
bool IsStarted () const
 Is thread started. More...
 
bool Start ()
 Start the thread. More...
 
bool Stop ()
 Safely stop the thread. More...
 
std::thread::id ThreadID () const
 Get this thread's thread ID. More...
 
std::thread::native_handle_type NativeHandle () const
 Get the underlying std::thread's native handle. More...
 

Private Types

using thread_function_t = std::function< void()>
 Typedef defining functor for virtual thread functions.
 

Private Member Functions

void ThreadIteration () NO_EXCEPT_ override
 Execute a single iteration of the thread. More...
 
void ProcessTerminationConditions () NO_EXCEPT_ override
 Perform any special termination actions. More...
 

Private Attributes

thread_function_t m_threadFunction
 Functor to call in the ThreadFunction method.
 
thread_function_t m_processTerminationConditions
 Functor to call in the ProcessTerminationConditions method.
 

Additional Inherited Members

- Protected Member Functions inherited from core_lib::threads::ThreadBase
 ThreadBase ()=default
 Default constructor.
 
void SetTerminating (bool terminating=true)
 Set terminating flag. More...
 
bool IsTerminating () const
 Is thread terminating. More...
 
void SleepForTime (unsigned int milliSecs) const
 Make this thread sleep for a period of time. More...
 

Detailed Description

ThreadRunner class.

When writing new code a user is most likely going to derive their own derieved class from ThreadBase but sometimes it may be preferable to have a thread runner object as a member variable of a class that is to have threaded out functionality. In which case ThreadRunner can be used.

Constructor & Destructor Documentation

◆ ThreadRunner()

core_lib::threads::ThreadRunner::ThreadRunner ( const thread_function_t threadFunction,
const thread_function_t processTerminationConditions,
bool  autoStart = false 
)

Initialisation constructor.

Parameters
[in]threadFunction- Functor to call in the ThreadFunction method.
[in]processTerminationConditions- Functor to call in the ProcessTerminationConditions method.
[in]autoStart- (Optional) Automatically call Start() at end of constructor.

The functors should not throw any exceptions.

Member Function Documentation

◆ ProcessTerminationConditions()

void core_lib::threads::ThreadRunner::ProcessTerminationConditions ( )
overrideprivatevirtual

Perform any special termination actions.

This function performs no actions in the base class definition but can be overriden in the dervied class to perform any special termination actions that are required after the terminting flag is set but before we call join on our underlying std::thread object.

Reimplemented from core_lib::threads::ThreadBase.

◆ SleepThreadForTime()

void core_lib::threads::ThreadRunner::SleepThreadForTime ( unsigned int  milliSecs) const

Make this thread sleep for a period of time.

Parameters
[in]milliSecs- Time period in milliseconds.

This function throws a std::runtime_error exception if thread not fully started and therefore cannot be made to sleep.

◆ ThreadIteration()

void core_lib::threads::ThreadRunner::ThreadIteration ( )
overrideprivatevirtual

Execute a single iteration of the thread.

This function is purely virtual and must be defined in the derived class. ThreadBase::Run continually loops until the thread is stopped or destructed so this function will be called each time the Run function loops round. Hence this function can be thought of what needs to be run in a single iteratation of the threads run loop.

Implements core_lib::threads::ThreadBase.


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