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.cpp
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 // Thread handler
22 //=============================================================================
23 
24 #include "simplethreadhandler.h"
25 #include <chrono>
26 #include "../system/log.h"
27 
28 
29 //------------------ Constructor, clear cancel flag ---------------------------
30 
35 
37  : mCancelThread(false),
38  mRunning(false)
39 {}
40 
41 
42 //------------------ Destructor ------------------- ---------------------------
43 
47 
49 {
50  stop();
51 }
52 
53 
54 //--------------- threadsafe cancelThread setter function----------------------
55 
59 
61 {
62  std::lock_guard<std::mutex> lock(mLockMutex); // protect remainder of block
63  mCancelThread = b;
64 }
65 
66 
67 //--------------- threadsafe cancelThread getter function----------------------
68 
72 
74 {
75  std::lock_guard<std::mutex> lock(mLockMutex); // protect remainder of block
76  return mCancelThread;
77 }
78 
79 
80 //-------------------------- Start the thread ---------------------------------
81 
86 
88 {
89  stop();
90  setCancelThread(false);
92 }
93 
94 
95 //------------------- Mark the thread as to be cancelled ---------------------
96 
102 
104 {
105  setCancelThread(true); // Set cancel flag to true
106  if (mThread.joinable()) mThread.join(); // Wait for thread to terminate
107 }
108 
109 
110 //--------------- Let the thread sleep in an idle state ----------------------
111 
117 
118 void SimpleThreadHandler::msleep(double milliseconds)
119 {
120  std::this_thread::sleep_for(
121  std::chrono::microseconds(static_cast<int>(1000*milliseconds)));
122 }
123 
124 
125 //--------------- Returns whether the thread is running ----------------------
126 
130 
132 {
133  return mRunning;
134 }
135 
136 
137 //--- called when an exception was caught during the worker function ---------
138 
144 
146 {
147  // empty
148  (void)e;
149 }
150 
151 
152 //------------------------------ Set thread name ------------------------------
153 
163 
165 {
166 #if defined(__linux__) and not defined(QT_NO_DEBUG) and not defined(__ANDROID__)
167  // Note: If you encounter a compiler error in the following line
168  // please comment it out. It serves only for debugging purposes on Qt.
169  pthread_setname_np (pthread_self(), s.c_str());
170 #else
171  if (s.size()){}; // suppress warning if inactive
172 #endif
173 }
174 
175 
176 // -------------------------- Private helper function -------------------------
177 
182 
184 {
185  mRunning = true;
186 
187  try
188  {
189  workerFunction();
190  }
191  catch (const EptException &e)
192  {
193  LogE("Worker thread stopped with EptException: %s", e.what());
194  exceptionCaught(e);
195  }
196  catch (const std::exception &e)
197  {
198  LogE("Worker thread stopped with std::exception: %s", e.what());
199  }
200  catch (...) {
201  LogE("Worker thread stopped with an unknown exception");
202  }
203 
204  mRunning = false;
205 }
virtual void stop()
Stop the thread.
bool mCancelThread
Cancel flag.
const char * what() const
Override std::exception::what.
Definition: eptexception.h:109
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.
#define LogE(...)
Definition: log.h:62
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.