ThreadPool.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * ThreadPool.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "BlockingQueue.h"
  12. #include <boost/thread/future.hpp>
  13. #include <boost/thread/condition_variable.hpp>
  14. VCMI_LIB_NAMESPACE_BEGIN
  15. typedef std::function<void()> TRMGfunction ;
  16. typedef std::optional<TRMGfunction> TRMGJob;
  17. //Credit to https://github.com/Liam0205/toy-threadpool/tree/master/yuuki
  18. class DLL_LINKAGE ThreadPool
  19. {
  20. private:
  21. using Lock = std::unique_lock<boost::shared_mutex>;
  22. mutable boost::shared_mutex mx;
  23. mutable std::condition_variable_any cv;
  24. mutable boost::once_flag once;
  25. bool isInitialized = false;
  26. bool stopping = false;
  27. bool canceling = false;
  28. public:
  29. ThreadPool();
  30. ~ThreadPool();
  31. void init(size_t numThreads);
  32. void spawn();
  33. void terminate();
  34. void cancel();
  35. public:
  36. bool initialized() const;
  37. bool running() const;
  38. int size() const;
  39. private:
  40. bool isRunning() const;
  41. public:
  42. auto async(std::function<void()>&& f) const -> boost::future<void>;
  43. private:
  44. std::vector<boost::thread> workers;
  45. mutable BlockingQueue<TRMGfunction> tasks;
  46. };
  47. ThreadPool::ThreadPool() :
  48. once(BOOST_ONCE_INIT)
  49. {};
  50. ThreadPool::~ThreadPool()
  51. {
  52. terminate();
  53. }
  54. inline void ThreadPool::init(size_t numThreads)
  55. {
  56. boost::call_once(once, [this, numThreads]()
  57. {
  58. Lock lock(mx);
  59. stopping = false;
  60. canceling = false;
  61. workers.reserve(numThreads);
  62. for (size_t i = 0; i < numThreads; ++i)
  63. {
  64. workers.emplace_back(std::bind(&ThreadPool::spawn, this));
  65. }
  66. isInitialized = true;
  67. });
  68. }
  69. bool ThreadPool::isRunning() const
  70. {
  71. return isInitialized && !stopping && !canceling;
  72. }
  73. inline bool ThreadPool::initialized() const
  74. {
  75. Lock lock(mx);
  76. return isInitialized;
  77. }
  78. inline bool ThreadPool::running() const
  79. {
  80. Lock lock(mx);
  81. return isRunning();
  82. }
  83. inline int ThreadPool::size() const
  84. {
  85. Lock lock(mx);
  86. return workers.size();
  87. }
  88. inline void ThreadPool::spawn()
  89. {
  90. while(true)
  91. {
  92. bool pop = false;
  93. TRMGfunction task;
  94. {
  95. Lock lock(mx);
  96. cv.wait(lock, [this, &pop, &task]
  97. {
  98. pop = tasks.pop(task);
  99. return canceling || stopping || pop;
  100. });
  101. }
  102. if (canceling || (stopping && !pop))
  103. {
  104. return;
  105. }
  106. task();
  107. }
  108. }
  109. inline void ThreadPool::terminate()
  110. {
  111. {
  112. Lock lock(mx);
  113. if (isRunning())
  114. {
  115. stopping = true;
  116. }
  117. else
  118. {
  119. return;
  120. }
  121. }
  122. cv.notify_all();
  123. for (auto& worker : workers)
  124. {
  125. worker.join();
  126. }
  127. }
  128. inline void ThreadPool::cancel()
  129. {
  130. {
  131. Lock lock(mx);
  132. if (running())
  133. {
  134. canceling = true;
  135. }
  136. else
  137. {
  138. return;
  139. }
  140. }
  141. tasks.clear();
  142. cv.notify_all();
  143. for (auto& worker : workers)
  144. {
  145. worker.join();
  146. }
  147. }
  148. auto ThreadPool::async(std::function<void()>&& f) const -> boost::future<void>
  149. {
  150. using TaskT = boost::packaged_task<void>;
  151. {
  152. Lock lock(mx);
  153. if (stopping || canceling)
  154. {
  155. throw std::runtime_error("Delegating task to a threadpool that has been terminated or canceled.");
  156. }
  157. }
  158. auto task = std::make_shared<TaskT>(f);
  159. boost::future<void> fut = task->get_future();
  160. tasks.emplace([task]() -> void
  161. {
  162. (*task)();
  163. });
  164. cv.notify_one();
  165. return fut;
  166. }
  167. VCMI_LIB_NAMESPACE_END