Main Page | Namespace List | Class Hierarchy | Class List | File List | Class Members | File Members

PtThreadPool.h

Go to the documentation of this file.
00001 #ifndef PtThreadPool_H 00002 #define PtThreadPool_H 00003 00004 #include "Exceptions.h" 00005 #include "ClosableFifoQueue.h" 00006 #include "Runnable.h" 00007 #include "PtMutex.h" 00008 #include <pthread.h> 00009 #include <queue> 00010 #include <set> 00011 #include <string> 00012 00013 BOREALIS_NAMESPACE_BEGIN; 00014 00015 class PtThreadPool 00016 { 00017 public: 00018 // The thread pool will start with minThreads, but will launch up to a 00019 // total of maxThreads threads. After a Runnable has been executed by one 00020 // of this pool's threads, the address of the Runnable is inserted into 00021 // the 'executedRunnables' queue. When the 'executedRunnables' queue 00022 // becomes 'closed', then this PtThreadPool has fully quiesced. 00023 PtThreadPool(int minThreads, 00024 int maxThreads, 00025 ClosableFifoQueue<Runnable*> & executedRunnables) 00026 throw (std::exception); 00027 00028 virtual ~PtThreadPool(); 00029 00030 // Schedules the runnable for execution by this thread pool. Between the 00031 // submission of a runnable, and its re-appearance of its address in the 00032 // 'executedRunnables' queue, the caller may not call any method on the 00033 // Runnable object and may not delete it. 00034 // 00035 // Runnables are assigned to threads in this pool in a FIFO manner. 00036 void schedule(Runnable * pRunnable) 00037 throw (std::exception); 00038 00039 // Disables future invocations of the 'schedule(...)' method. The next 00040 // time that the number of Runnables scheduled for this pool goes down 00041 // to zero, this pool will call the 'close()' method on the 00042 // 'execeutedRunnables' queue. 00043 void quiesce() 00044 throw (std::exception); 00045 00046 private: 00047 static void *threadFunc(void *pPool); 00048 00049 // Creates a server thread, which then enters the cycle of awaiting 00050 // the availability if an object in _scheduledRunnables. 00051 void createServerThread() 00052 throw (std::exception); 00053 00054 // This is just an object-oriented version of threadFunc(...). 00055 void serverThreadMethod(); 00056 00057 ClosableFifoQueue<Runnable*> _scheduledRunnables; 00058 ClosableFifoQueue<Runnable*> & _executedRunnables; 00059 00060 int _minThreads; 00061 int _maxThreads; 00062 int _numCurrentThreads; 00063 int _numPendingRunnables; 00064 // bool _quiescing; 00065 00066 PtMutex _mtx; // General guardian for this class' methods. 00067 00068 set<pthread_t> _threads; 00069 00070 // Posted once all server threads have finished any interesting work they're 00071 // going to do, *and* aren't going to make any more attempts to acquire _mtx. 00072 BinarySem _allThreadsQuiescing; 00073 }; 00074 00075 BOREALIS_NAMESPACE_END; 00076 00077 #endif

Generated on Fri Nov 12 15:15:21 2004 for Borealis by doxygen 1.3.8