tc_thread_pool.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #include "util/tc_thread_pool.h"
  17. #include "util/tc_common.h"
  18. #include <iostream>
  19. namespace tars
  20. {
  21. TC_ThreadPool::TC_ThreadPool()
  22. : _threadNum(1), _bTerminate(true)
  23. {
  24. }
  25. TC_ThreadPool::~TC_ThreadPool()
  26. {
  27. stop();
  28. }
  29. void TC_ThreadPool::init(size_t num)
  30. {
  31. std::unique_lock<std::mutex> lock(_mutex);
  32. if (!_threads.empty())
  33. {
  34. throw TC_ThreadPool_Exception("[TC_ThreadPool::init] thread pool has start!");
  35. }
  36. _threadNum = num;
  37. }
  38. void TC_ThreadPool::stop()
  39. {
  40. if(_bTerminate)
  41. {
  42. return ;
  43. }
  44. {
  45. std::unique_lock<std::mutex> lock(_mutex);
  46. _bTerminate = true;
  47. _condition.notify_all();
  48. }
  49. for (size_t i = 0; i < _threads.size(); i++)
  50. {
  51. if(_threads[i]->joinable())
  52. {
  53. _threads[i]->join();
  54. }
  55. delete _threads[i];
  56. _threads[i] = NULL;
  57. }
  58. std::unique_lock<std::mutex> lock(_mutex);
  59. _threads.clear();
  60. }
  61. void TC_ThreadPool::start()
  62. {
  63. std::unique_lock<std::mutex> lock(_mutex);
  64. if (!_threads.empty())
  65. {
  66. throw TC_ThreadPool_Exception("[TC_ThreadPool::start] thread pool has start!");
  67. }
  68. _bTerminate = false;
  69. for (size_t i = 0; i < _threadNum; i++)
  70. {
  71. _threads.push_back(new thread(&TC_ThreadPool::run, this));
  72. }
  73. }
  74. bool TC_ThreadPool::get(TaskFuncPtr& task)
  75. {
  76. std::unique_lock<std::mutex> lock(_mutex);
  77. if (_tasks.empty())
  78. {
  79. _condition.wait(lock, [this] { return _bTerminate || !_tasks.empty(); });
  80. }
  81. if (_bTerminate)
  82. return false;
  83. if (!_tasks.empty())
  84. {
  85. task = std::move(_tasks.front());
  86. _tasks.pop();
  87. return true;
  88. }
  89. return false;
  90. }
  91. void TC_ThreadPool::run()
  92. {
  93. //调用处理部分
  94. while (!isTerminate())
  95. {
  96. TaskFuncPtr task;
  97. bool ok = get(task);
  98. if (ok)
  99. {
  100. ++_atomic;
  101. try
  102. {
  103. if (task->_expireTime != 0 && task->_expireTime < TNOWMS )
  104. {
  105. //超时任务,是否需要处理?
  106. }
  107. else
  108. {
  109. task->_func();
  110. }
  111. }
  112. catch (...)
  113. {
  114. }
  115. --_atomic;
  116. //任务都执行完毕了
  117. std::unique_lock<std::mutex> lock(_mutex);
  118. if (_atomic == 0 && _tasks.empty())
  119. {
  120. _condition.notify_all();
  121. }
  122. }
  123. }
  124. }
  125. bool TC_ThreadPool::waitForAllDone(int millsecond)
  126. {
  127. std::unique_lock<std::mutex> lock(_mutex);
  128. if (_tasks.empty() && _atomic == 0)
  129. return true;
  130. if (millsecond < 0)
  131. {
  132. _condition.wait(lock, [this] { return _tasks.empty() && _atomic == 0; });
  133. return true;
  134. }
  135. else
  136. {
  137. return _condition.wait_for(lock, std::chrono::milliseconds(millsecond), [this] { return _tasks.empty() && _atomic == 0; });
  138. }
  139. }
  140. ///////////////////////////////////////////////////////////////////////////////////////
  141. TC_ThreadPoolHash::TC_ThreadPoolHash()
  142. {
  143. }
  144. TC_ThreadPoolHash::~TC_ThreadPoolHash()
  145. {
  146. }
  147. void TC_ThreadPoolHash::init(size_t num)
  148. {
  149. for (size_t i = 0 ;i < num;i++)
  150. {
  151. TC_ThreadPool* p = new TC_ThreadPool();
  152. p->init(1);
  153. _pools.push_back(p);
  154. }
  155. }
  156. TC_ThreadPool* TC_ThreadPoolHash::getThread(size_t index)
  157. {
  158. if (_pools.empty() || (index + 1) > _pools.size())
  159. {
  160. return nullptr;
  161. }
  162. return _pools[index];
  163. }
  164. TC_ThreadPool* TC_ThreadPoolHash::selectThread(const string& hashkey)
  165. {
  166. if (_pools.empty())
  167. {
  168. return nullptr;
  169. }
  170. std::hash<string> hash_fu;
  171. size_t pos = hash_fu(hashkey) % _pools.size();
  172. return _pools[pos];
  173. }
  174. void TC_ThreadPoolHash::stop()
  175. {
  176. for (size_t i = 0; i < _pools.size(); i++)
  177. {
  178. _pools[i]->stop();
  179. delete _pools[i];
  180. }
  181. _pools.clear();
  182. }
  183. void TC_ThreadPoolHash::start()
  184. {
  185. for (size_t i = 0; i < _pools.size() ;i ++)
  186. {
  187. _pools[i]->start();
  188. }
  189. }
  190. }