ThreadPool.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. ==========================================================================
  3. * 类ThreadPool是本代码的核心类,类中自动维护线程池的创建和任务队列的派送
  4. * 其中的TaskFun是任务函数
  5. * 其中的TaskCallbackFun是回调函数
  6. *用法:定义一个ThreadPool变量,TaskFun函数和TaskCallbackFun回调函数,然后调用ThreadPool的QueueTaskItem()函数即可
  7. Author: TTGuoying
  8. Date: 2018/02/19 23:15
  9. ==========================================================================
  10. */
  11. #pragma once
  12. #include <Windows.h>
  13. #include <list>
  14. #include <queue>
  15. #include <memory>
  16. using std::list;
  17. using std::queue;
  18. using std::shared_ptr;
  19. #define THRESHOLE_OF_WAIT_TASK 20
  20. typedef int(*TaskFun)(PVOID param); // 任务函数
  21. typedef void(*TaskCallbackFun)(/*int*/PVOID result); // 回调函数
  22. class ThreadPool
  23. {
  24. private:
  25. // 线程类(内部类)
  26. class Thread
  27. {
  28. public:
  29. Thread(ThreadPool *threadPool);
  30. ~Thread();
  31. BOOL isBusy(); // 是否有任务在执行
  32. void ExecuteTask(TaskFun task, PVOID param, TaskCallbackFun taskCallback); // 执行任务
  33. private:
  34. ThreadPool *threadPool; // 所属线程池
  35. BOOL busy; // 是否有任务在执行
  36. BOOL exit; // 是否退出
  37. HANDLE thread; // 线程句柄
  38. TaskFun task; // 要执行的任务
  39. PVOID param; // 任务参数
  40. TaskCallbackFun taskCb; // 回调的任务
  41. static unsigned int __stdcall ThreadProc(PVOID pM); // 线程函数
  42. };
  43. // IOCP的通知种类
  44. enum WAIT_OPERATION_TYPE
  45. {
  46. GET_TASK,
  47. EXIT
  48. };
  49. // 待执行的任务类
  50. class WaitTask
  51. {
  52. public:
  53. WaitTask(TaskFun task, PVOID param, TaskCallbackFun taskCb, BOOL bLong)
  54. {
  55. this->task = task;
  56. this->param = param;
  57. this->taskCb = taskCb;
  58. this->bLong = bLong;
  59. }
  60. ~WaitTask() { task = NULL; taskCb = NULL; bLong = FALSE; param = NULL; }
  61. TaskFun task; // 要执行的任务
  62. PVOID param; // 任务参数
  63. TaskCallbackFun taskCb; // 回调的任务
  64. BOOL bLong; // 是否时长任务
  65. };
  66. // 从任务列表取任务的线程函数
  67. static unsigned int __stdcall GetTaskThreadProc(PVOID pM)
  68. {
  69. ThreadPool *threadPool = (ThreadPool *)pM;
  70. BOOL bRet = FALSE;
  71. DWORD dwBytes = 0;
  72. OVERLAPPED *ol;
  73. while (true)
  74. {
  75. WaitForSingleObject(threadPool->stopEvent, INFINITE);
  76. threadPool->opType_cs.Lock();
  77. WAIT_OPERATION_TYPE opType = threadPool->opType;
  78. threadPool->opType_cs.UnLock();
  79. // 收到退出标志
  80. if (EXIT == (DWORD)opType)
  81. {
  82. break;
  83. }
  84. else if (GET_TASK == (DWORD)opType)
  85. {
  86. threadPool->GetTaskExcute();
  87. }
  88. Sleep(150);
  89. }
  90. return 0;
  91. }
  92. //线程临界区锁
  93. class CriticalSectionLock
  94. {
  95. private:
  96. CRITICAL_SECTION cs;//临界区
  97. public:
  98. CriticalSectionLock() { InitializeCriticalSection(&cs); }
  99. ~CriticalSectionLock() { DeleteCriticalSection(&cs); }
  100. void Lock() { EnterCriticalSection(&cs); }
  101. void UnLock() { LeaveCriticalSection(&cs); }
  102. };
  103. public:
  104. ThreadPool(size_t minNumOfThread = 2, size_t maxNumOfThread = 10);
  105. ~ThreadPool();
  106. void init(size_t minNumOfThread, size_t maxNumOfThread);
  107. BOOL QueueTaskItem(TaskFun task, PVOID param, TaskCallbackFun taskCb = NULL, BOOL longFun = FALSE); // 任务入队
  108. public:
  109. size_t getCurNumOfThread() { return getIdleThreadNum() + getBusyThreadNum(); } // 获取线程池中的当前线程数
  110. size_t GetMaxNumOfThread() { return maxNumOfThread - numOfLongFun; } // 获取线程池中的最大线程数
  111. void SetMaxNumOfThread(size_t size) // 设置线程池中的最大线程数
  112. {
  113. if (size < numOfLongFun)
  114. {
  115. maxNumOfThread = size + numOfLongFun;
  116. }
  117. else
  118. maxNumOfThread = size;
  119. }
  120. size_t GetMinNumOfThread() { return minNumOfThread; } // 获取线程池中的最小线程数
  121. void SetMinNumOfThread(size_t size) { minNumOfThread = size; } // 设置线程池中的最小线程数
  122. size_t getIdleThreadNum() { return idleThreadList.size(); } // 获取线程池中的线程数
  123. size_t getBusyThreadNum() { return busyThreadList.size(); } // 获取线程池中的线程数
  124. void CreateIdleThread(size_t size); // 创建空闲线程
  125. void DeleteIdleThread(size_t size); // 删除空闲线程
  126. Thread *GetIdleThread(); // 获取空闲线程
  127. void MoveBusyThreadToIdleList(Thread *busyThread); // 忙碌线程加入空闲列表
  128. void MoveThreadToBusyList(Thread *thread); // 线程加入忙碌列表
  129. void GetTaskExcute(); // 从任务队列中取任务执行
  130. WaitTask *GetTask(); // 从任务队列中取任务
  131. CriticalSectionLock idleThreadLock; // 空闲线程列表锁
  132. list<Thread *> idleThreadList; // 空闲线程列表
  133. CriticalSectionLock busyThreadLock; // 忙碌线程列表锁
  134. list<Thread *> busyThreadList; // 忙碌线程列表
  135. CriticalSectionLock waitTaskLock;
  136. list<WaitTask *> waitTaskList; // 任务列表
  137. WAIT_OPERATION_TYPE opType;
  138. CriticalSectionLock opType_cs;
  139. HANDLE dispatchThrad; // 分发任务线程
  140. HANDLE stopEvent; // 通知线程退出的时间
  141. size_t maxNumOfThread; // 线程池中最大的线程数
  142. size_t minNumOfThread; // 线程池中最小的线程数
  143. size_t numOfLongFun; // 线程池中最小的线程数
  144. };