hthreadpool.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #ifndef HV_THREAD_POOL_H_
  2. #define HV_THREAD_POOL_H_
  3. #include <vector>
  4. #include <thread>
  5. #include <queue>
  6. #include <functional>
  7. #include <atomic>
  8. #include <mutex>
  9. #include <condition_variable>
  10. #include <future>
  11. #include <memory>
  12. #include <utility>
  13. class HThreadPool {
  14. public:
  15. using Task = std::function<void()>;
  16. HThreadPool(int size = std::thread::hardware_concurrency())
  17. : pool_size(size), idle_num(size), status(STOP) {
  18. }
  19. ~HThreadPool() {
  20. stop();
  21. }
  22. int start() {
  23. if (status == STOP) {
  24. status = RUNNING;
  25. for (int i = 0; i < pool_size; ++i) {
  26. workers.emplace_back(std::thread([this]{
  27. while (status != STOP) {
  28. while (status == PAUSE) {
  29. std::this_thread::yield();
  30. }
  31. Task task;
  32. {
  33. std::unique_lock<std::mutex> locker(_mutex);
  34. _cond.wait(locker, [this]{
  35. return status == STOP || !tasks.empty();
  36. });
  37. if (status == STOP) return;
  38. if (!tasks.empty()) {
  39. --idle_num;
  40. task = std::move(tasks.front());
  41. tasks.pop();
  42. }
  43. }
  44. task();
  45. ++idle_num;
  46. }
  47. }));
  48. }
  49. }
  50. return 0;
  51. }
  52. int stop() {
  53. if (status != STOP) {
  54. status = STOP;
  55. _cond.notify_all();
  56. for (auto& worker : workers) {
  57. worker.join();
  58. }
  59. }
  60. return 0;
  61. }
  62. int pause() {
  63. if (status == RUNNING) {
  64. status = PAUSE;
  65. }
  66. return 0;
  67. }
  68. int resume() {
  69. if (status == PAUSE) {
  70. status = RUNNING;
  71. }
  72. return 0;
  73. }
  74. int wait() {
  75. while (1) {
  76. if (status == STOP || (tasks.empty() && idle_num == pool_size)) {
  77. break;
  78. }
  79. std::this_thread::yield();
  80. }
  81. return 0;
  82. }
  83. // return a future, calling future.get() will wait task done and return RetType.
  84. // commit(fn, args...)
  85. // commit(std::bind(&Class::mem_fn, &obj))
  86. // commit(std::mem_fn(&Class::mem_fn, &obj))
  87. template<class Fn, class... Args>
  88. auto commit(Fn&& fn, Args&&... args) -> std::future<decltype(fn(args...))> {
  89. using RetType = decltype(fn(args...));
  90. auto task = std::make_shared<std::packaged_task<RetType()> >(
  91. std::bind(std::forward<Fn>(fn), std::forward<Args>(args)...));
  92. std::future<RetType> future = task->get_future();
  93. {
  94. std::lock_guard<std::mutex> locker(_mutex);
  95. tasks.emplace([task]{
  96. (*task)();
  97. });
  98. }
  99. _cond.notify_one();
  100. return future;
  101. }
  102. public:
  103. enum Status {
  104. STOP,
  105. RUNNING,
  106. PAUSE,
  107. };
  108. int pool_size;
  109. std::atomic<int> idle_num;
  110. std::atomic<Status> status;
  111. std::vector<std::thread> workers;
  112. std::queue<Task> tasks;
  113. protected:
  114. std::mutex _mutex;
  115. std::condition_variable _cond;
  116. };
  117. #endif // HV_THREAD_POOL_H_