ThreadPool.cc 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include <boost/detail/atomic_count.hpp>
  2. #include <glog/logging.h>
  3. #include "Thread/ThreadPool.h"
  4. namespace Hainan {
  5. ThreadPool::ThreadPool(int num) :
  6. thread_num(num), running(false), work_num(0)
  7. {
  8. if (num == 0)
  9. {
  10. thread_num = boost::thread::hardware_concurrency();
  11. }
  12. }
  13. ThreadPool::~ThreadPool()
  14. {
  15. }
  16. void ThreadPool::Start()
  17. {
  18. running = true;
  19. for (int i = 0; i < thread_num; ++i)
  20. {
  21. ThreadPtr t(new boost::thread(
  22. boost::bind(&ThreadPool::Runner, this)));
  23. threads.push_back(t);
  24. t->detach();
  25. ++work_num;
  26. }
  27. }
  28. void ThreadPool::Stop()
  29. {
  30. VLOG(3)<< "Stop";
  31. boost::mutex::scoped_lock lock(mutex);
  32. running = false;
  33. cond.notify_all();
  34. while (work_num > 0)
  35. {
  36. VLOG(3) << "done tasks size = " << tasks.size();
  37. done.wait(lock);
  38. }
  39. }
  40. void ThreadPool::Runner()
  41. {
  42. VLOG(3) << "thread start";
  43. bool continued = true;
  44. while (continued)
  45. {
  46. boost::function<void (void)> task;
  47. {
  48. VLOG(3) << "loop lock";
  49. boost::mutex::scoped_lock lock(mutex);
  50. VLOG(3) << "loop lock ok";
  51. while (running && tasks.empty())
  52. {
  53. cond.wait(lock);
  54. VLOG(3) << "cond";
  55. }
  56. if (!tasks.empty())
  57. {
  58. VLOG(3) << "fetch task";
  59. task = tasks.front();
  60. tasks.pop_front();
  61. }
  62. continued = running || !tasks.empty();
  63. VLOG(3) << "continued = " << continued
  64. << "running = " << running
  65. << " tasks size = " << tasks.size();
  66. VLOG(3) << "loop unlock";
  67. }
  68. if (task)
  69. {
  70. task();
  71. }
  72. }
  73. if (--work_num == 0)
  74. {
  75. VLOG(3) << "work_num = " << work_num;
  76. done.notify_one();
  77. }
  78. }
  79. bool ThreadPool::PushTask(boost::function<void (void)> task)
  80. {
  81. VLOG(3) << "push task";
  82. {
  83. boost::mutex::scoped_lock lock(mutex);
  84. if (!running)
  85. {
  86. return false;
  87. }
  88. tasks.push_back(task);
  89. }
  90. VLOG(3) << "push task unlock";
  91. cond.notify_one();
  92. return true;
  93. }
  94. } // namespace Hainan