00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef __mitkThreadMaster_h
00011 #define __mitkThreadMaster_h
00012
00013 #include "mitkObject.h"
00014
00015 class mitkThreadDelegate;
00023 class MITK_COMMON_API mitkThreadWorker :public mitkObject
00024 {
00025 public:
00026 MITK_TYPE(mitkThreadWorker,mitkObject)
00027 mitkThreadWorker(){}
00028 friend class mitkThreadMaster;
00029 protected:
00030 virtual ~mitkThreadWorker(){}
00036 virtual int TasksPerTime() {return 1;}
00041 virtual void SendTaskBag(void *taskBag) {}
00050 virtual void DoTask(int taskID,int taskNumber)=0;
00055 virtual mitkThreadWorker *Duplicate()=0;
00056 private:
00057 mitkThreadWorker(const mitkThreadWorker&);
00058 void operator = (const mitkThreadWorker&);
00059 };
00060
00068 class MITK_COMMON_API mitkThreadWorkerCreator : public mitkObject
00069 {
00070 public:
00071 MITK_TYPE(mitkThreadWorkerCreator,mitkObject)
00072 mitkThreadWorkerCreator(){}
00077 virtual int GetWorkerNum();
00083 virtual mitkThreadWorker* CreateThreadWorker(int WorkerID)=0;
00084 protected:
00085 virtual ~mitkThreadWorkerCreator(){}
00086 private:
00087 mitkThreadWorkerCreator(const mitkThreadWorkerCreator&);
00088 void operator = (const mitkThreadWorkerCreator&);
00089 };
00090
00091 class mitkThread;
00092 class mitkConditionVariable;
00093 #include "mitkMutex.h"
00102 class MITK_COMMON_API mitkThreadMaster : public mitkObject
00103 {
00104 public:
00105 MITK_TYPE(mitkThreadMaster,mitkObject)
00106 mitkThreadMaster(mitkThreadWorkerCreator* workerCreator);
00107 mitkThreadMaster(mitkThreadWorker* sampleWorker);
00108 mitkThreadMaster(int threadNumber,mitkThreadWorker* sampleWorker);
00113 int GetThreadNumber() const
00114 {
00115 return m_threadNumber;
00116 }
00126 void DoTasks(unsigned long taskNumber,unsigned long startID=0,void *taskBag=NULL);
00127
00128 protected:
00129 virtual ~mitkThreadMaster();
00130
00131 private:
00132 mitkThreadMaster (const mitkThreadMaster&);
00133 void operator = (const mitkThreadMaster&);
00134
00135 int m_threadNumber;
00136 mitkThreadDelegate** m_threads;
00137 int *m_threadIDs;
00138
00139 mitkThreadWorker **m_workers;
00140
00141 unsigned long m_taskNumber;
00142 unsigned long m_taskProgress;
00143 unsigned long m_taskStartID;
00144
00145 int m_toDoValue,m_doneValue;
00146 mitkConditionVariable *m_toDoCV;
00147 mitkConditionVariable *m_doneCV;
00148 mitkMutex *m_progressMutex;
00149
00150 void _init(mitkThreadWorker* sampleWorker);
00151 unsigned long _getProgress(int step)
00152 {
00153 unsigned long prog;
00154 m_progressMutex->Lock();
00155 prog=m_taskProgress;
00156 m_taskProgress+=step;
00157 m_progressMutex->Unlock();
00158 return prog;
00159 }
00160 void _threadEntrance(void *param);
00161
00162 };
00163
00164 #endif
00165