comparison thread/Thread.h @ 67:516c86946900

* Pull out AsynchronousTask into its own header * Add a fixed-size block allocator based on FSBAllocator
author cannam
date Thu, 14 May 2009 12:45:08 +0000
parents 2af6edd98dfa
children 5f88f517b637
comparison
equal deleted inserted replaced
66:2af6edd98dfa 67:516c86946900
144 #ifdef DEBUG_CONDITION 144 #ifdef DEBUG_CONDITION
145 std::string m_name; 145 std::string m_name;
146 #endif 146 #endif
147 }; 147 };
148 148
149 class AsynchronousTask : public Thread
150 {
151 public:
152 AsynchronousTask() :
153 m_todo("AsynchronousTask: task to perform"),
154 m_done("AsynchronousTask: task complete"),
155 m_inTask(false),
156 m_finishing(false)
157 {
158 start();
159 }
160 virtual ~AsynchronousTask()
161 {
162 m_finishing = true;
163 m_todo.signal();
164 wait();
165 }
166
167 // subclass must provide methods to request task and obtain
168 // results
169
170 protected:
171 void startTask() {
172 m_todo.lock();
173 m_inTask = true;
174 m_todo.signal();
175 m_done.lock();
176 m_todo.unlock();
177 }
178 void awaitTask() {
179 while (m_inTask) m_done.wait();
180 m_done.unlock();
181 }
182
183 virtual void performTask() = 0;
184
185 private:
186 virtual void run() {
187 m_todo.lock();
188 while (!m_finishing) {
189 while (!m_inTask && !m_finishing) {
190 m_todo.wait();
191 }
192 if (m_finishing) {
193 break;
194 }
195 if (m_inTask) {
196 performTask();
197 m_inTask = false;
198 m_done.signal();
199 }
200 }
201 m_todo.unlock();
202 }
203
204 Condition m_todo;
205 Condition m_done;
206 bool m_inTask;
207 bool m_finishing;
208 };
209
210 #endif 149 #endif