|
Core Library
1.7.0.0
Library containing core utilities and tools for threading, networking, logging, INI and CSV file management etc.
|
Thread base class. More...
#include <ThreadBase.h>
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. | |
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.
| bool core_lib::threads::ThreadBase::IsStarted | ( | ) | const |
Is thread started.
|
protected |
Is thread terminating.
| std::thread::native_handle_type core_lib::threads::ThreadBase::NativeHandle | ( | ) | const |
Get the underlying std::thread's native handle.
This function throws a std::runtime_error exception if thread not fully started and so has not got a handle assigned.
|
protectedvirtual |
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.
|
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.
|
private |
Set started flag.
| [in] | started | - True if started, false otherwise. |
|
protected |
Set terminating flag.
| [in] | terminating | - True if terminating, false otherwise. |
|
private |
Store thread ID and native handle.
| [in] | threadId | - Thread ID. |
| [in] | nativeHandle | - Native handle. |
|
protected |
Make this thread sleep for a period of time.
| [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.
| bool core_lib::threads::ThreadBase::Start | ( | ) |
Start the thread.
Typically called at the end of the derived classes constructor.
| bool core_lib::threads::ThreadBase::Stop | ( | ) |
Safely stop the thread.
Typically called at the start of the derived classes destructor.
| std::thread::id core_lib::threads::ThreadBase::ThreadID | ( | ) | const |
Get this thread's thread ID.
This function throws a std::runtime_error exception if thread not fully started and so has not got a valid ID.
|
protectedpure 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.