Shadowrun: Awakened 29 September 2011 - Build 871
ThreadPool.h
Go to the documentation of this file.
00001 #ifndef __THREADPOOL_H
00002 #define __THREADPOOL_H
00003 
00004 #include <boost/thread/thread.hpp>
00005 
00006 #include "CodeGenMacros.h"
00007 
00008 const size_t DefaultMaxThreadsInThreadPool = 25;
00009 const size_t DefaultTaskCountThresholdForNewThread = 5;
00010 const boost::xtime::xtime_sec_t ManagerWaitTimeInSeconds = 1;
00011 const boost::xtime::xtime_sec_t WorkerActiveWaitTimeInSeconds = 0;
00012 const boost::xtime::xtime_sec_t WorkerIdleWaitTimeInSeconds = 1;
00013 
00014 namespace Support
00015 {
00019     class ThreadTaskBase
00020     {
00021     public:
00026         virtual void execute() = 0;
00027     };
00028 
00032     class ThreadPoolTaskBase : public ThreadTaskBase
00033     {
00034     protected:
00035         bool _deleteOnCompletion;
00036 
00037     public:
00041         ThreadPoolTaskBase() : _deleteOnCompletion(true) { }
00042 
00043         //Gets or sets whether or not this task should be deleted by the pool after execution
00044         //Default value is TRUE
00045         INLINE GET(getDelete, bool, _deleteOnCompletion);
00046         INLINE SET(setDelete, bool, _deleteOnCompletion);
00047     };
00048 
00055     class ThreadPool : boost::noncopyable
00056     {
00057     protected:
00061         struct ThreadPoolWorker
00062         {
00063             ThreadPool& _threadPool;
00064             boost::xtime _waitTimeActive;
00065             boost::xtime _waitTimeIdle;
00066             
00067             ThreadPoolWorker(ThreadPool& threadPool);
00068 
00069             void operator()();
00070         };
00071 
00075         struct ThreadPoolManager
00076         {
00077             ThreadPool& _threadPool;
00078             boost::xtime _waitTime;
00079 
00080             ThreadPoolManager(ThreadPool& threadPool) : _threadPool(threadPool) 
00081             {
00082                 _waitTime.sec = ManagerWaitTimeInSeconds;
00083             }
00084 
00085             void operator()();
00086         };
00087 
00092         mutable boost::mutex _taskMutex;
00093 
00098         std::list<ThreadPoolTaskBase*> _tasks;
00099 
00103         boost::thread_group _threads;
00104 
00105         size_t _maxThreadCount;
00106         size_t _newThreadThreshold;
00107 
00108         bool SafeDequeue(ThreadPoolTaskBase** item);
00109 
00110         virtual void createWorkerThread();
00111         virtual void createManagerThread();
00112 
00113         void checkGrowPool();
00114         bool consumeTask();
00115         void stop();
00116 
00117     public:
00118 
00119         //max threads is also +1 to accomodate a manager thread that is created to spawn other threads
00120         ThreadPool();
00121         ThreadPool(size_t threadCount);
00122         ThreadPool(size_t maxThreads, size_t growThreshold);
00123 
00124         virtual ~ThreadPool();
00125 
00126         void queueTask(ThreadPoolTaskBase* task);
00127 
00128         size_t taskCount();
00129 
00130         void clearTasks();
00131     };
00132 }
00133 
00134 #endif

Copyright © 2007-2010 by The Shadowrun: Awakened Team. This work is licensed under the GNU Lesser General Public License 3.

GNU Lesser General Public License 3 Sourceforge.net