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.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * Copyright 2015 Haye Hinrichsen, Christoph Wick
3  *
4  * This file is part of Entropy Piano Tuner.
5  *
6  * Entropy Piano Tuner is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by the
8  * Free Software Foundation, either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * Entropy Piano Tuner is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * Entropy Piano Tuner. If not, see http://www.gnu.org/licenses/.
18  *****************************************************************************/
19 
20 //=============================================================================
21 // Simple thread handler
22 //=============================================================================
23 
24 #ifndef SIMPLETHREADHANDLER_H
25 #define SIMPLETHREADHANDLER_H
26 
27 #include <mutex>
28 #include <thread>
29 #include <atomic>
30 #include "eptexception.h"
31 
32 #define CHECK_CANCEL_THREAD { if (cancelThread()) {return;} }
33 #define CANCEL_THREAD { setCancelThread(true); return; }
34 
52 
54 {
55 public:
57  virtual ~SimpleThreadHandler();
58 
59  virtual void start();
60  virtual void stop();
61 
62  static void setThreadName(std::string s);
63 
64 protected:
65 
76 
77  virtual void workerFunction() = 0;
78 
84 
85  virtual void exceptionCaught(const EptException &e);
86 
87  void setCancelThread(bool b);
88  bool cancelThread() const;
89  void msleep(double milliseconds);
90  bool isThreadRunning() const;
91 
92 private:
93  void simpleWorkerFunction();
94 
96  std::atomic<bool> mRunning;
97  std::thread mThread;
98  mutable std::mutex mLockMutex;
99 };
100 
101 #endif // SIMPLETHREADHANDLER_H
virtual void stop()
Stop the thread.
bool mCancelThread
Cancel flag.
void msleep(double milliseconds)
Sleep function for staying idle.
SimpleThreadHandler()
Constructor.
virtual void exceptionCaught(const EptException &e)
EPT exception handling.
virtual ~SimpleThreadHandler()
virtual destructor calls stop
bool isThreadRunning() const
Flag to check if the thread is running.
static void setThreadName(std::string s)
Specify the name of the thread.
std::thread mThread
Local thread member variable.
virtual void start()
Start the thread.
void simpleWorkerFunction()
Private helper function.
void setCancelThread(bool b)
Cancel-flag setter method, thread-safe.
std::mutex mLockMutex
Mutex protecting the cancel flag.
std::atomic< bool > mRunning
Is the thread running.
virtual void workerFunction()=0
Virtual worker function, executed within the new thread.
bool cancelThread() const
Cancel-flag getter method, thread-safe.
Simple thread handler.