Entropy Piano Tuner  1.1.3 (documentation not yet complete)
An open-source experimental software for piano tuning by entropy minimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
SimpleThreadHandler Class Referenceabstract

Simple thread handler. More...

#include <simplethreadhandler.h>

+ Inheritance diagram for SimpleThreadHandler:

Public Member Functions

 SimpleThreadHandler ()
 Constructor. More...
 
virtual ~SimpleThreadHandler ()
 virtual destructor calls stop More...
 
virtual void start ()
 Start the thread. More...
 
virtual void stop ()
 Stop the thread. More...
 

Static Public Member Functions

static void setThreadName (std::string s)
 Specify the name of the thread. More...
 

Protected Member Functions

virtual void workerFunction ()=0
 Virtual worker function, executed within the new thread. More...
 
virtual void exceptionCaught (const EptException &e)
 EPT exception handling. More...
 
void setCancelThread (bool b)
 Cancel-flag setter method, thread-safe. More...
 
bool cancelThread () const
 Cancel-flag getter method, thread-safe. More...
 
void msleep (double milliseconds)
 Sleep function for staying idle. More...
 
bool isThreadRunning () const
 Flag to check if the thread is running. More...
 

Private Member Functions

void simpleWorkerFunction ()
 Private helper function. More...
 

Private Attributes

bool mCancelThread
 Cancel flag. More...
 
std::atomic< bool > mRunning
 Is the thread running. More...
 
std::thread mThread
 Local thread member variable. More...
 
std::mutex mLockMutex
 Mutex protecting the cancel flag. More...
 

Detailed Description

Simple thread handler.

This class simplifies the handling of C11 threads.

Usage: The class creating the thread has to be derived from SimpleThreadHandler and to overload the pure virtual workerFunction() with the code to be executed within the thread. The worker function should either return by itself or when cancelThread() becomes true. This means that the workerFunction() usually contains a while(not cancelThread()) loop.

The thread is started by calling start(). By calling stop() the cancel flag is set and the thread will terminate after some time depending on the implementation of the workerFunction(). For keeping the thread idle in the workerFunction() call the member function msleep().

Definition at line 53 of file simplethreadhandler.h.

Constructor & Destructor Documentation

SimpleThreadHandler::SimpleThreadHandler ( )

Constructor.

The constructor sets the cancel-thread flag to false. It does not yet start the thread.

Definition at line 36 of file simplethreadhandler.cpp.

SimpleThreadHandler::~SimpleThreadHandler ( )
virtual

virtual destructor calls stop

The destructor waits for the current thread to stop if it is running.

Definition at line 48 of file simplethreadhandler.cpp.

+ Here is the call graph for this function:

Member Function Documentation

bool SimpleThreadHandler::cancelThread ( ) const
protected

Cancel-flag getter method, thread-safe.

Find out whether a thread has been registered for termination.

Definition at line 73 of file simplethreadhandler.cpp.

void SimpleThreadHandler::exceptionCaught ( const EptException e)
protectedvirtual

EPT exception handling.

Called when an exception is caught during the worker function

Reimplement this function if you want a custom exception handling, e.g. restart the thread. By default this function does nothing.

Parameters
e: The exception that was caught

Definition at line 145 of file simplethreadhandler.cpp.

bool SimpleThreadHandler::isThreadRunning ( ) const
protected

Flag to check if the thread is running.

Returns
Boolean telling whether the thread is running

Definition at line 131 of file simplethreadhandler.cpp.

void SimpleThreadHandler::msleep ( double  milliseconds)
protected

Sleep function for staying idle.

Call this function from the thread if you want to let it stay in an idle state without consuming CPU time.

Parameters
time: Waiting time (double) in milliseconds.

Definition at line 118 of file simplethreadhandler.cpp.

void SimpleThreadHandler::setCancelThread ( bool  b)
protected

Cancel-flag setter method, thread-safe.

Register a thread for termination.

Definition at line 60 of file simplethreadhandler.cpp.

void SimpleThreadHandler::setThreadName ( std::string  s)
static

Specify the name of the thread.

With this function it is possible to rename the thread. This is particularly useful for debugging since the Qt-creator shows the thread name in a list. However, there is no platform-independent implementation of thread name handling. Therefore, this function works only on Linux using the underlying pthread implementation. The function has to be called within the thread itself, i.e., in the worker function. It works only in debug mode.

Parameters
s: name of the thread

Definition at line 164 of file simplethreadhandler.cpp.

void SimpleThreadHandler::simpleWorkerFunction ( )
private

Private helper function.

This function calls the abstract workerFunction of the implementation. It also catches its exceptions-

Definition at line 183 of file simplethreadhandler.cpp.

+ Here is the call graph for this function:

void SimpleThreadHandler::start ( )
virtual

Start the thread.

If the thread is already running register it for termination and wait until it has terminated. Then restart a new thread.

Definition at line 87 of file simplethreadhandler.cpp.

+ Here is the call graph for this function:

void SimpleThreadHandler::stop ( )
virtual

Stop the thread.

Mark the thread for cancellation. This function waits for termination of the thread, i.e. it blocks until the thread has been shut down. This waiting time will depend on the implementation of the workerFunction.

Reimplemented in SignalAnalyzer.

Definition at line 103 of file simplethreadhandler.cpp.

+ Here is the call graph for this function:

virtual void SimpleThreadHandler::workerFunction ( )
protectedpure virtual

Virtual worker function, executed within the new thread.

Overload this function with the code to be executed within the new thread. Make sure that it either returns by itself or when cancelThread() becomes true. This means that a typical implementation of the workerFunction() should contain a while(not cancelThread()) loop.

If you want the thread to stay idle call msleep.

Implemented in SignalAnalyzer, WaveformGenerator, KeyRecognizer, and Algorithm.

Member Data Documentation

bool SimpleThreadHandler::mCancelThread
private

Cancel flag.

Definition at line 95 of file simplethreadhandler.h.

std::mutex SimpleThreadHandler::mLockMutex
mutableprivate

Mutex protecting the cancel flag.

Definition at line 98 of file simplethreadhandler.h.

std::atomic<bool> SimpleThreadHandler::mRunning
private

Is the thread running.

Definition at line 96 of file simplethreadhandler.h.

std::thread SimpleThreadHandler::mThread
private

Local thread member variable.

Definition at line 97 of file simplethreadhandler.h.


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