Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef PARALLELLOOP_H
00029 #define PARALLELLOOP_H
00030
00031 #include <time.h>
00032
00033 #include "QGpCoreToolsDLLExport.h"
00034 #include "Thread.h"
00035
00036 namespace QGpCoreTools {
00037
00038 class LoopTask;
00039
00040 class QGPCORETOOLS_EXPORT ParallelLoop : public QObject
00041 {
00042 Q_OBJECT
00043 public:
00044 ParallelLoop(QObject * parent=0) : QObject(parent) {}
00045
00046 void start(int iStart, int iEnd, bool forceParallel=false);
00047 void terminate();
00048 void waitFinished();
00049
00050 protected:
00051 virtual LoopTask * newTask()=0;
00052 protected slots:
00053 virtual void taskFinished();
00054 private slots:
00055 void statusChanged(QString msg);
00056 void progressChanged(int value);
00057 void progressInit(int maximumValue);
00058 signals:
00059 void statusChanged(int processIndex, QString msg);
00060 void progressChanged(int processIndex, int progress);
00061 void progressInit(int processIndex, int maximumValue);
00062 void finished();
00063 private:
00064 inline LoopTask * newTask(int iEnd);
00065 struct TaskInfo {
00066 int index;
00067 int maximumProgress;
00068 };
00069 QMap<LoopTask *, TaskInfo> _tasks;
00070 QMutex _indexMutex;
00071 int _index;
00072 };
00073
00074 class QGPCORETOOLS_EXPORT LoopTask : public Thread
00075 {
00076 Q_OBJECT
00077 public:
00078 LoopTask() : Thread() {_terminated=false;}
00079
00080 void terminate() {_terminated.fetchAndStoreOrdered(true);}
00081 bool terminated() {return _terminated.testAndSetOrdered(true,true);}
00082
00083 void setIndex(int * index, QMutex * mutex) {_index=index; _indexMutex=mutex;}
00084 void setEndIndex(int index) {_endIndex=index;}
00085 protected:
00086 virtual void run();
00087 virtual void run(int index)=0;
00088 void setProgressValue(int value);
00089 void setProgressMaximum(int value) {emit progressInit(value);}
00090 void setStatus(QString msg) {emit statusChanged(msg);}
00091 int endIndex() const {return _endIndex;}
00092 signals:
00093 void statusChanged(QString msg);
00094 void progressInit(int maximumValue);
00095 void progressChanged(int value);
00096 private:
00097 QAtomicInt _terminated;
00098 QMutex * _indexMutex;
00099 int * _index;
00100 int _endIndex;
00101 QAtomicInt _lastProgressTime;
00102 };
00103
00104 inline void LoopTask::setProgressValue(int value)
00105 {
00106 TRACE;
00107 int now=time(0);
00108 if(_lastProgressTime.fetchAndStoreOrdered(now)!=now) {
00109 progressChanged(value);
00110 }
00111 }
00112
00113 }
00114
00115 #endif // PARALLELLOOP_H