Core Library 1.7.0.0 Library containing core utilities and tools for threading, networking, logging, INI and CSV file management etc.

Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | List of all members
core_lib::threads::ThreadBase Class Referenceabstract

Thread base class. More...

`#include <ThreadBase.h>`

Inheritance diagram for core_lib::threads::ThreadBase:

## Public Member Functions
| ThreadBase (const ThreadBase &)=delete
| Copy constructor deleted.
ThreadBase & | operator= (const ThreadBase &)=delete
| Copy assignment operator deleted.
| ThreadBase (ThreadBase &&)=default
| Move constructor deleted.
ThreadBase & | operator= (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...

## Protected Member Functions
| 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...
virtual void | ThreadIteration () NO_EXCEPT_=0
| Execute a single iteration of the thread. More...
virtual void | ProcessTerminationConditions () NO_EXCEPT_
| Perform any special termination actions. More...

## Private Member Functions
void | SetThreadIdAndNativeHandle (const std::thread::id &threadId, const std::thread::native_handle_type &nativeHandle)
| Store thread ID and native handle. More...
void | SetStarted (bool started=true)
| Set started flag. More...
void | Run ()
| Run the thread's iterations in a loop. More...

## Private Attributes
std::mutex | m_mutex
| Access mutex to protect private data.
bool | m_started {false}
| Boolean flag to mark thread as started.
bool | m_terminating {false}
| Boolean flag to mark thread as terminating.
std::thread::id | m_threadId {}
| Thread ID of started thread object.
std::thread::native_handle_type | m_nativeHandle {}
| Native thread handle (where supported) of started thread.
std::thread | m_thread {}
| Underlying std::thread object.

## Detailed Description

Thread base class.

This abstract class can be used as a base class for objects that need to be threaded. It neatly wraps all the useful functionality of std::thread in a usable way.

## Member Function Documentation

## ◆ IsStarted()

bool core_lib::threads::ThreadBase::IsStarted | ( |  | ) | const

Is thread started.

Returns
  Returns true if started, false otherwise.

## ◆ IsTerminating()

bool core_lib::threads::ThreadBase::IsTerminating | ( |  | ) | const
| protected

Is thread terminating.

Returns
  Returns true if terminating, false otherwise.

## ◆ NativeHandle()

std::thread::native_handle_type core_lib::threads::ThreadBase::NativeHandle | ( |  | ) | const

Get the underlying std::thread's native handle.

Returns
  Returns native thread handle if supported.

This function throws a std::runtime_error exception if thread not fully started and so has not got a handle assigned.

## ◆ ProcessTerminationConditions()

void core_lib::threads::ThreadBase::ProcessTerminationConditions | ( |  | ) |
| protected virtual

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 in core_lib::threads::MessageQueueThread< MessageId, MessageType >, core_lib::threads::ThreadRunner, and core_lib::threads::EventThread.

## ◆ Run()

void core_lib::threads::ThreadBase::Run | ( |  | ) |
| private

Run the thread's iterations in a loop.

This function loops calling ThreadIteration() to perform a single iterations actions. It stops looping when the thread is termainted.

## ◆ SetStarted()

void core_lib::threads::ThreadBase::SetStarted | ( | bool | started = `true` | ) |
| private

Set started flag.

Parameters

  [in] | started | - True if started, false otherwise.

## ◆ SetTerminating()

void core_lib::threads::ThreadBase::SetTerminating | ( | bool | terminating = `true` | ) |
| protected

Set terminating flag.

Parameters

  [in] | terminating | - True if terminating, false otherwise.

## ◆ SetThreadIdAndNativeHandle()

void core_lib::threads::ThreadBase::SetThreadIdAndNativeHandle | ( | const std::thread::id & | threadId,
|  | const std::thread::native_handle_type & | nativeHandle
| ) |  |
| private

Store thread ID and native handle.

Parameters

  [in] | threadId | - Thread ID.
  [in] | nativeHandle | - Native handle.

## ◆ SleepForTime()

void core_lib::threads::ThreadBase::SleepForTime | ( | unsigned int | milliSecs | ) | const
| protected

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.

## ◆ Start()

bool core_lib::threads::ThreadBase::Start | ( |  | ) |

Start the thread.

Returns
  True if started, false otherwise.

Typically called at the end of the derived classes constructor.

## ◆ Stop()

bool core_lib::threads::ThreadBase::Stop | ( |  | ) |

Safely stop the thread.

Returns
  True if started, false otherwise.

Typically called at the start of the derived classes destructor.

## ◆ ThreadID()

std::thread::id core_lib::threads::ThreadBase::ThreadID | ( |  | ) | const

Get this thread's thread ID.

Returns
  Returns thread ID.

This function throws a std::runtime_error exception if thread not fully started and so has not got a valid ID.

## ◆ ThreadIteration()

virtual void core_lib::threads::ThreadBase::ThreadIteration | ( |  | ) |
| protected pure virtual

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.

Implemented in core_lib::threads::MessageQueueThread< MessageId, MessageType >, core_lib::threads::ThreadRunner, and core_lib::threads::EventThread.

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

- D:/Projects/CoreLibrary/Include/Threads/ThreadBase.h
- D:/Projects/CoreLibrary/Source/Threads/ThreadBase.cpp