ThreadPool.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //
  2. // Copyright @ 2016 Hangzhou Topzen Ltd.
  3. // Author: Wu Jiubao (wujb@hztopzen.com) @ 2016-09
  4. //
  5. #ifndef __THREADPOOL_H
  6. #define __THREADPOOL_H
  7. #include <list>
  8. #include "Thread.h"
  9. #include "Semaphore.h"
  10. #include "BaseTypes.h"
  11. #include "Locks.h"
  12. namespace tzc {
  13. class CWorkerThread;
  14. class CJob;
  15. // thread pool, manage all threads
  16. class DECLDLL CThreadPool : public tzc::OSThread {
  17. friend class CWorkerThread;
  18. #ifdef THREADPOOL_SINGLETON
  19. private:
  20. CThreadPool();
  21. virtual ~CThreadPool();
  22. #else
  23. public:
  24. CThreadPool();
  25. CThreadPool(TZ_Uint32 num);
  26. virtual ~CThreadPool();
  27. #endif
  28. public:
  29. #ifdef THREADPOOL_SINGLETON
  30. static CThreadPool * GetInstance(void);
  31. static void DeleteInstance(void);
  32. #endif
  33. TZ_INT Initialize();
  34. void SetMaxThreadNum(TZ_Uint32 maxNum);
  35. TZ_Uint32 GetMaxThreadNum(void);
  36. void SetLowIdleThreadNum(TZ_Uint32 minNum);
  37. TZ_Uint32 GetLowIdleThreadNum(void);
  38. void SetHighIdleThreadNum(TZ_Uint32 highNum);
  39. TZ_Uint32 GetHighIdleThreadNum(void);
  40. TZ_Uint32 GetIdleThreadNum(void);
  41. TZ_Uint32 GetAllThreadNum(void);
  42. TZ_Uint32 GetBusyThreadNum(void);
  43. void SetInitThreadNum(TZ_Uint32 initNum);
  44. TZ_Uint32 GetInitThreadNum(void);
  45. void TerminateAll(void);
  46. void PushJob(CJob * job);
  47. protected:
  48. virtual void Entry();
  49. TZ_INT setJob2WorkerThread(CJob * job);
  50. CWorkerThread * getIdleThread(void);
  51. void appendToIdleList(CWorkerThread * jobthread);
  52. void moveToBusyList(CWorkerThread * idlethread);
  53. void moveToIdleList(CWorkerThread * busythread);
  54. void deleteIdleThread(TZ_Uint32 num);
  55. void createIdleThread(TZ_Uint32 num);
  56. private:
  57. #ifdef THREADPOOL_SINGLETON
  58. static CThreadPool * _instance;
  59. static tzc::Mutex _insMutex;
  60. #endif
  61. tzc::Mutex m_busyMutex; // used for operating m_busyList
  62. tzc::Mutex m_idleMutex; // used for operating m_idleList
  63. tzc::Mutex m_jobMutex; // used for getting idle thread to do job
  64. std::list<CWorkerThread *> m_threadList; // all threads in thread pool
  65. std::list<CWorkerThread *> m_busyList; // busy threads in thread pool
  66. std::list<CWorkerThread *> m_idleList; // idle threads in thread pool
  67. std::list<CJob *> m_jobList;
  68. TZ_Uint32 m_maxNum; // max number of thread
  69. TZ_Uint32 m_availLow; // the lowest available threads in thread pool
  70. TZ_Uint32 m_availHigh; // the highest available threads in thread pool
  71. TZ_Uint32 m_availNum; // current available threads in thread pool
  72. TZ_Uint32 m_initNum; // init number of thread
  73. TZ_Uint32 m_jobNum; // job number in the m_jobList
  74. };
  75. inline void CThreadPool::SetMaxThreadNum(TZ_Uint32 maxNum)
  76. {
  77. m_maxNum = maxNum;
  78. }
  79. inline TZ_Uint32 CThreadPool::GetMaxThreadNum(void)
  80. {
  81. return m_maxNum;
  82. }
  83. inline void CThreadPool::SetLowIdleThreadNum(TZ_Uint32 minNum)
  84. {
  85. m_availLow = minNum;
  86. }
  87. inline TZ_Uint32 CThreadPool::GetLowIdleThreadNum(void)
  88. {
  89. return m_availLow;
  90. }
  91. inline void CThreadPool::SetHighIdleThreadNum(TZ_Uint32 highNum)
  92. {
  93. m_availHigh = highNum;
  94. }
  95. inline TZ_Uint32 CThreadPool::GetHighIdleThreadNum(void)
  96. {
  97. return m_availHigh;
  98. }
  99. inline TZ_Uint32 CThreadPool::GetIdleThreadNum(void)
  100. {
  101. return m_availNum;
  102. }
  103. inline TZ_Uint32 CThreadPool::GetAllThreadNum(void)
  104. {
  105. ScopedLock lock(m_idleMutex);
  106. return m_threadList.size();
  107. }
  108. inline TZ_Uint32 CThreadPool::GetBusyThreadNum(void)
  109. {
  110. ScopedLock lock(m_busyMutex);
  111. return m_busyList.size();
  112. }
  113. inline void CThreadPool::SetInitThreadNum(TZ_Uint32 initNum)
  114. {
  115. m_initNum = initNum;
  116. }
  117. inline TZ_Uint32 CThreadPool::GetInitThreadNum(void)
  118. {
  119. return m_initNum;
  120. }
  121. // work thread, all threads start to wait for job when be constructed
  122. class DECLDLL CWorkerThread : public tzc::OSThread
  123. {
  124. friend class CThreadPool;
  125. private:
  126. CWorkerThread(CThreadPool * thrPool);
  127. virtual ~CWorkerThread();
  128. virtual void Entry();
  129. void StartWork(CJob * job);
  130. void StopWork(void);
  131. private:
  132. CThreadPool * m_threadPool;
  133. CJob * m_job;
  134. TZ_BOOL m_bExit;
  135. tzc::Semaphore m_jobCond;
  136. };
  137. // base class of Job, all concrete jobs should inherit it and implement the Run()
  138. class DECLDLL CJob {
  139. public:
  140. CJob(const std::string & jobName, void * jobData);
  141. virtual ~CJob();
  142. TZ_Uint32 GetJobNo(void);
  143. TZ_BOOL ToDeleted(void);
  144. virtual void Run() = 0;
  145. protected:
  146. static TZ_Uint32 _jobNo;
  147. static tzc::Mutex _jobNoMutex;
  148. TZ_Uint32 m_jobNo;
  149. TZ_BOOL m_toDeleted;
  150. std::string m_jobName;
  151. void * m_jobData;
  152. };
  153. } // namespace tzc
  154. #endif /*-----#ifndef _THREADPOOL_H-----*/