LAMA
|
00001 00033 #ifndef LAMA_THREAD_POOL_HPP_ 00034 #define LAMA_THREAD_POOL_HPP_ 00035 00036 // for dll_import 00037 #include <lama/config.hpp> 00038 00039 // logging 00040 #include <logging/logging.hpp> 00041 00042 // boost 00043 #include <boost/shared_ptr.hpp> 00044 #include <boost/function.hpp> 00045 #include <boost/version.hpp> 00046 00047 #include <boost/thread/thread.hpp> 00048 #include <boost/thread/mutex.hpp> 00049 #include <boost/thread/condition.hpp> 00050 00051 #include <climits> 00052 #include <queue> 00053 00054 namespace lama { 00055 00058 typedef unsigned int TaskId; 00059 00067 struct LAMA_DLL_IMPORTEXPORT LAMAThreadTask 00068 { 00069 enum TaskState { DEFINED, QUEUED, RUNNING, FINISHED }; 00070 00071 boost::function<void()> mWork; 00072 00073 volatile TaskState mState; 00074 00075 bool mException; 00076 00077 unsigned int mTaskId; 00078 00079 int ompThreads; 00080 00083 static boost::shared_ptr<LAMAThreadTask> create( boost::function<void()> work, unsigned int taskId, int numOmpThreads = 0 ); 00084 }; 00085 00096 class LAMA_DLL_IMPORTEXPORT LAMAThreadPool 00097 { 00098 00099 public: 00100 00103 LAMAThreadPool( int size ); 00104 00113 boost::shared_ptr<LAMAThreadTask> schedule( boost::function<void()> work, int numOmpThreads = 0 ); 00114 00117 void wait( boost::shared_ptr<LAMAThreadTask> task ); 00118 00121 ~LAMAThreadPool(); 00122 00123 private: 00124 00127 void worker( int id ); 00128 00129 LAMA_LOG_DECL_STATIC_LOGGER( logger ); 00130 00131 unsigned int mTaskId; // last given task id 00132 00133 int mMaxSize; // number of worker threads 00134 00135 std::vector<boost::thread*> mThreads; // worker threads of this pool 00136 00137 std::queue<boost::shared_ptr<LAMAThreadTask> > mTaskQueue; 00138 00139 boost::condition mNotifyFinished; // notify about finished tasks 00140 boost::condition mNotifyTask; // notify about new task 00141 00142 boost::mutex mTaskQueueMutex; // make access to taskqueue thread-safe 00143 boost::mutex mNotifyFinishMutex; // used for wait on mNotifyFinished 00144 00145 enum WorkerState { WORKING, WAITING }; 00146 00147 WorkerState mWorkerState; //used to indicate the state of the worker 00148 }; 00149 00150 } // namespace lama 00151 00152 #endif // LAMA_THREAD_POOL_HPP_