Overte C++ Documentation
GenericThread.h
1 //
2 // GenericThread.h
3 // libraries/shared/src
4 //
5 // Created by Brad Hefta-Gaub on 8/12/13.
6 // Copyright 2013 High Fidelity, Inc.
7 //
8 // Generic Threaded or non-threaded processing class.
9 //
10 // Distributed under the Apache License, Version 2.0.
11 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
12 //
13 
14 #ifndef hifi_GenericThread_h
15 #define hifi_GenericThread_h
16 
17 #include <QtCore/QObject>
18 #include <QMutex>
19 #include <QThread>
20 
23 class GenericThread : public QObject {
24  Q_OBJECT
25 public:
26  GenericThread();
27  virtual ~GenericThread();
28 
31  void initialize(bool isThreaded = true, QThread::Priority priority = QThread::NormalPriority);
32 
34  void terminate();
35 
36  virtual void terminating() { }; // lets your subclass know we're terminating, and it should respond appropriately
37 
38  bool isThreaded() const { return _isThreaded; }
39 
41  virtual bool process() = 0;
42  virtual void setup() {};
43  virtual void shutdown() {};
44 
45 public slots:
47  void threadRoutine();
48 
49 signals:
50  void started();
51  void finished();
52 
53 protected:
54 
56  void lock() { _mutex.lock(); }
57 
59  void unlock() { _mutex.unlock(); }
60 
61  bool isStillRunning() const { return !_stopThread; }
62 
63 protected:
64  QMutex _mutex;
65 
66  bool _stopThread;
67  bool _isThreaded;
68  QThread* _thread;
69 };
70 
71 #endif // hifi_GenericThread_h
Definition: GenericThread.h:23
void initialize(bool isThreaded=true, QThread::Priority priority=QThread::NormalPriority)
Definition: GenericThread.cpp:33
virtual bool process()=0
Override this function to do whatever your class actually does, return false to exit thread early.
void lock()
Locks all the resources of the thread.
Definition: GenericThread.h:56
void unlock()
Unlocks all the resources of the thread.
Definition: GenericThread.h:59
void terminate()
Call to stop the thread.
Definition: GenericThread.cpp:59
void threadRoutine()
If you're running in non-threaded mode, you must call this regularly.
Definition: GenericThread.cpp:75