00001 /* -*- mode: c++; c-basic-offset: 4; -*- */ 00002 #ifndef SENDS_THREAD_POOL_HH 00003 #define SENDS_THREAD_POOL_HH 00004 #include <pthread.h> 00005 #include "gateway.hh" 00006 00007 00008 namespace sends { 00009 00024 class thread_pool { 00025 public: 00032 static void* thread_exec(void* tp); 00033 00037 typedef unsigned int count_type; 00038 00039 public: 00046 thread_pool(count_type num); 00047 00051 virtual ~thread_pool(void); 00052 00057 count_type capacity(void) const; 00058 00064 count_type count(void) const; 00065 00069 virtual void thread_entry(void); 00070 00075 count_type get_busy(void) const; 00076 00081 count_type get_max_busy(void) const; 00082 00086 void lock(void); 00087 00092 void make_threads(void); 00093 00098 void set_size(count_type N); 00099 00102 count_type size(void) const; 00103 00107 void unlock(void); 00108 00112 void wait_empty(void); 00113 00114 protected: 00120 count_type busy(void); 00121 00126 void done(void); 00127 00133 bool test_run(void); 00134 00135 private: 00136 thread::gateway gate; 00137 count_type max_threads; 00138 count_type nthread; 00139 count_type nbusy; 00140 count_type use_count; 00141 00142 //----------------------------- Statistics 00143 count_type max_busy; 00144 }; 00145 00146 //================================= Inline methods. 00147 inline thread_pool::count_type 00148 thread_pool::get_busy(void) const { 00149 return nbusy; 00150 } 00151 00152 inline thread_pool::count_type 00153 thread_pool::get_max_busy(void) const { 00154 return max_busy; 00155 } 00156 00157 inline thread_pool::count_type 00158 thread_pool::count(void) const { 00159 return use_count; 00160 } 00161 00162 inline thread_pool::count_type 00163 thread_pool::capacity(void) const { 00164 return max_threads; 00165 } 00166 00167 inline thread_pool::count_type 00168 thread_pool::size(void) const { 00169 return nthread; 00170 } 00171 00172 } 00173 00174 #endif // !defined(SENDS_THREAD_POOL_HH)
1.5.5