-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtthread.h
More file actions
49 lines (39 loc) · 1.2 KB
/
tthread.h
File metadata and controls
49 lines (39 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#if !defined(TTHREAD_H)
#define TTHREAD_H
#include <QThread>
#include <QDebug>
//*****************************************************************************
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
class TThread : public QThread
{
Q_OBJECT
private:
volatile bool mExit;
std::wstring mThreadName;
virtual void run() Q_DECL_OVERRIDE { while(!onExec()) { } }
protected:
TThread(const std::wstring& threadName) : QThread(), mExit(false), mThreadName(threadName) {}
~TThread()
{
if(!threadExit())
threadFinish();
}
void setThreadExit() { mExit = true; }
virtual bool onExec() = 0;
bool threadFinish()
{
setThreadExit();
bool status = wait(5000);
if(!status) {
qDebug() << "[ERROR] TThread" << QString::fromStdWString(threadName()) << "bad exit";
} else {
qDebug() << "[INFO] TThread" << QString::fromStdWString(threadName()) << "normal exit";
}
return status;
}
public:
bool threadExit() const { return mExit; }
const std::wstring& threadName() const { return mThreadName; }
};
#endif // TTHREAD_H