1 #pragma once 2 3 #include <QAtomicInt> 4 #include <QObject> 5 6 // Waits for a number of tasks to complete and emits allTasksDone() once all are 7 // complete. 8 class TaskWatcher : public QObject { 9 Q_OBJECT 10 public: 11 TaskWatcher(QObject* pParent=NULL); 12 virtual ~TaskWatcher(); 13 14 // Increment the number of active tasks by one and watch pTask for 15 // doneSignal. This should be called before the task starts to prevent a 16 // race condition where the task completes before we listen for doneSignal. 17 void watchTask(); 18 19 // Called when an individual task is done. 20 // WARNING: Invoked in the thread the task runs in. 21 void taskDone(); 22 23 signals: 24 // Signaled when all watched tasks are done from the thread that emitted 25 // the last taskDone() signal. 26 void allTasksDone(); 27 28 private: 29 QAtomicInt m_activeTasks; 30 }; 31