ThreadPool.h

Go to the documentation of this file.
00001 #ifndef THREADPOOL_H_
00002 #define THREADPOOL_H_
00003 
00004 // #include <boost/function/function0.hpp>
00005 #include <boost/thread/thread.hpp>
00006 #include <boost/thread/mutex.hpp>
00007 #include <boost/thread/condition.hpp>
00008 #include <vector>
00009 
00010 using boost::condition;
00011 using boost::try_mutex;
00012 using boost::mutex;
00013 using boost::thread;
00014 
00015 
00016 class WorkerMainLoop ;
00017 
00018 class ThreadPoolJob
00019 {
00020 public :
00021     virtual void start() = 0;
00022     virtual ~ThreadPoolJob() {};
00023 };
00024 
00025 class WorkerState
00026 {
00027 
00028 public :
00029 
00030     WorkerState() ;
00031     virtual ~WorkerState() { };
00032 
00033     void assignJob(ThreadPoolJob &j);
00034 
00035     virtual void finish();
00036 
00037 private :
00038 
00039     bool toBeFinished ;
00040 
00041     ThreadPoolJob *job;
00042 
00043     friend class WorkerMainLoop ;
00044 };
00045 
00046 class WorkerMainLoop
00047 {
00048 public:
00049     WorkerMainLoop(WorkerState *st, boost::condition &c1, boost::mutex &m1,
00050                    boost::condition &c2, boost::mutex &m2);
00051 
00052     virtual ~WorkerMainLoop();
00053 
00054     void operator() ();
00055 
00056 protected:
00057 
00058     condition &cond1;
00059     mutex &mtx1;
00060 
00061 
00062     condition &cond2;
00063     mutex &mtx2;
00064 
00065     WorkerState *state ;
00066 };
00067 
00068 class ThreadPool
00069 {
00070 public:
00071     ThreadPool(int maxNumThreads);
00072 
00073     virtual ~ThreadPool();
00074 
00080     int dispatch(int id, ThreadPoolJob & f) ;
00081 
00085     void waitAll();
00086 
00088      int getNumThreads() {
00089          return _maxNumThreads;
00090      }
00091 
00092 protected :
00093     int _maxNumThreads ;
00094 
00095     std::vector<thread*> threads;
00096     std::vector<WorkerState*> workerstates;
00097     std::vector<condition*> cond1;
00098     std::vector<condition*> cond2;
00099     std::vector<mutex*> mtx1;
00100     std::vector<mutex*> mtx2;
00101 
00102 };
00103 
00104 #endif /*THREADPOOL_H_*/

Generated on Wed Jul 9 16:34:39 2008 for PCSIM by  doxygen 1.5.5