Entropy Piano Tuner
1.1.3 (documentation not yet complete)
An open-source experimental software for piano tuning by entropy minimization
|
Simple thread handler. More...
#include <simplethreadhandler.h>
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... | |
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.
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.
|
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.
|
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.
|
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.
e | : The exception that was caught |
Definition at line 145 of file simplethreadhandler.cpp.
|
protected |
Flag to check if the thread is running.
Definition at line 131 of file simplethreadhandler.cpp.
|
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.
time | : Waiting time (double) in milliseconds. |
Definition at line 118 of file simplethreadhandler.cpp.
|
protected |
Cancel-flag setter method, thread-safe.
Register a thread for termination.
Definition at line 60 of file simplethreadhandler.cpp.
|
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.
s | : name of the thread |
Definition at line 164 of file simplethreadhandler.cpp.
|
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.
|
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.
|
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.
|
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.
|
private |
Cancel flag.
Definition at line 95 of file simplethreadhandler.h.
|
mutableprivate |
Mutex protecting the cancel flag.
Definition at line 98 of file simplethreadhandler.h.
|
private |
Is the thread running.
Definition at line 96 of file simplethreadhandler.h.
|
private |
Local thread member variable.
Definition at line 97 of file simplethreadhandler.h.