CFEL - ASG Software Suite  2.5.0
CASS
pausablethread.h
Go to the documentation of this file.
1 /**
2  * @file pausablethread.h declaration of a pausable QThread
3  *
4  * @author Lutz Foucar
5  */
6 
7 #ifndef PAUSABLETHREAD_H
8 #define PAUSABLETHREAD_H
9 
10 #include <stdexcept>
11 
12 #include <QtCore/QThread>
13 #include <QtCore/QMutex>
14 #include <QtCore/QWaitCondition>
15 
16 namespace lmf
17 {
18 /** A QThread that has the ability to be paused and resumed
19  *
20  * This class inherits from QThread and enhances the QThread class to be able
21  * to be paused and resumed
22  *
23  * @todo check the logic whether this is threadsafe (maybe need to add some
24  * more mutexlockers.)
25  *
26  * @author Lutz Foucar
27  */
28 class PausableThread : public QThread
29 {
30  Q_OBJECT
31 public:
32  /** enum describing the internal status of the thread */
34 
35  /** enum describing the control status of the thread */
37 
38  /** enum describing which exception was thrown */
40  {
48  };
49 
50 public:
51  /** constructor
52  *
53  * You can construct this thread to be either paused or running when you
54  * call the start() member of the created object.
55  *
56  * @param control the initial state of the thread
57  * @param parent pointer to the parent object
58  */
59  PausableThread(control_t control=_run, QObject *parent = 0)
60  : QThread(parent),
62  _control(control),
63  _pausecount((control==_run?0:1)),
65  _invarg_excep(""),
66  _runt_excep(""),
67  _outrange_excep(""),
68  _logic_excep("")
69  {}
70 
71  /** destructor
72  *
73  * stops the threads execution before deleting the thread. Make sure that
74  * all waitconditions are properly shut down.
75  */
76  virtual ~PausableThread();
77 
78  /** run the thread
79  *
80  * this will call the pure virtual function runthis that needs to be
81  * implemented with the code that the thread should execute.
82  * It will handle all exceptions and ensures that they are not leaving the
83  * thread. One can check after exectution of the thread which exceptions have
84  * been thrown. Supported are
85  * std::invalid_argument
86  * std::runtime_error
87  * std::out_of_range
88  */
89  void run();
90 
91  /** contains the code to run in the thread
92  *
93  * needs to be implemented by the derived threads
94  */
95  virtual void runthis()=0;
96 
97  /** pause the thread
98  *
99  * Will tell the thread to pause. The function can be told to block until
100  * the thread is paused. It does that by waiting internaly on
101  * waitUntilPaused() to return. If one does not want this function to block,
102  * just call it with the default argument (false).
103  *
104  * @param block if true this function call will block until the thread is
105  * paused. If false this will return immidiatly (default).
106  */
107  void pause(bool block=false);
108 
109  /** waits until thread is paused
110  *
111  * Waits until the thread is paused by using the wait condition.
112  */
113  void waitUntilPaused();
114 
115  /** resume the thread
116  *
117  * Will tell the thread to resume by waking up the Condition
118  */
119  void resume();
120 
121  /** return the current status of the thread */
122  status_t status()const {return _status;}
123 
124  /** query whether this thread is told to quit
125  *
126  * @return true when it should quit, false otherwise
127  */
128  bool shouldQuit()const {return (_control == _quit);}
129 
130  /** rethrow the thrown exception
131  *
132  * this should only be called when the thread has quitted
133  */
134  void rethrowException()const;
135 
136  /** @return the exception thrown */
138 
139 public slots:
140  /** tell the thread to quit */
141  virtual void end() {_control = _quit;}
142 
143 protected:
144  /** point where the thread will be paused
145  *
146  * Call this function from within your run() at the point where you can
147  * pause the thread whithout leaving it in an undefined state. It will check
148  * whether the thread is requested to pause, if so it will pause the thread.
149  */
150  void pausePoint();
151 
152 protected:
153  /** mutex to wait on until thread is paused */
155 
156  /** wait condition to wait on until thread is resumed */
158 
159  /** wait condition to wait unitl thread is paused */
161 
162  /** the internal status of the thread */
164 
165  /** the internal control status of the thread */
167 
168  /** a counter how many threads have pause this thread */
169  size_t _pausecount;
170 
171  /** flag to show that general exception was thrown */
173 
174  /** the invalid arguemnt exception thrown */
175  std::invalid_argument _invarg_excep;
176 
177  /** the invalid arguemnt exception thrown */
178  std::runtime_error _runt_excep;
179 
180  /** the invalid arguemnt exception thrown */
181  std::out_of_range _outrange_excep;
182 
183  /** the invalid arguemnt exception thrown */
184  std::logic_error _logic_excep;
185 };
186 
187 }//end namespace lmf
188 #endif // PAUSABLETHREAD_H
std::out_of_range _outrange_excep
the invalid arguemnt exception thrown
void rethrowException() const
rethrow the thrown exception
status_t _status
the internal status of the thread
void pause(bool block=false)
pause the thread
status_t
enum describing the internal status of the thread
void waitUntilPaused()
waits until thread is paused
QWaitCondition _waitUntilPausedCondition
wait condition to wait unitl thread is paused
std::runtime_error _runt_excep
the invalid arguemnt exception thrown
std::invalid_argument _invarg_excep
the invalid arguemnt exception thrown
PausableThread(control_t control=_run, QObject *parent=0)
constructor
QMutex _pauseMutex
mutex to wait on until thread is paused
std::logic_error _logic_excep
the invalid arguemnt exception thrown
virtual ~PausableThread()
destructor
void run()
run the thread
exception_t _exception_thrown
flag to show that general exception was thrown
exception_t
enum describing which exception was thrown
virtual void end()
tell the thread to quit
bool shouldQuit() const
query whether this thread is told to quit
control_t _control
the internal control status of the thread
void resume()
resume the thread
virtual void runthis()=0
contains the code to run in the thread
control_t
enum describing the control status of the thread
void pausePoint()
point where the thread will be paused
QWaitCondition _pauseCondition
wait condition to wait on until thread is resumed
status_t status() const
return the current status of the thread
exception_t exceptionThrown() const
A QThread that has the ability to be paused and resumed.
size_t _pausecount
a counter how many threads have pause this thread