ThreadPool.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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<std::shared_mutex>;
  22. mutable std::shared_mutex mx;
  23. mutable std::condition_variable_any cv;
  24. mutable std::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. {};
  49. ThreadPool::~ThreadPool()
  50. {
  51. terminate();
  52. }
  53. inline void ThreadPool::init(size_t numThreads)
  54. {
  55. std::call_once(once, [this, numThreads]()
  56. {
  57. Lock lock(mx);
  58. stopping = false;
  59. canceling = false;
  60. workers.reserve(numThreads);
  61. for (size_t i = 0; i < numThreads; ++i)
  62. {
  63. workers.emplace_back(std::bind(&ThreadPool::spawn, this));
  64. }
  65. isInitialized = true;
  66. });
  67. }
  68. bool ThreadPool::isRunning() const
  69. {
  70. return isInitialized && !stopping && !canceling;
  71. }
  72. inline bool ThreadPool::initialized() const
  73. {
  74. Lock lock(mx);
  75. return isInitialized;
  76. }
  77. inline bool ThreadPool::running() const
  78. {
  79. Lock lock(mx);
  80. return isRunning();
  81. }
  82. inline int ThreadPool::size() const
  83. {
  84. Lock lock(mx);
  85. return workers.size();
  86. }
  87. inline void ThreadPool::spawn()
  88. {
  89. while(true)
  90. {
  91. bool pop = false;
  92. TRMGfunction task;
  93. {
  94. Lock lock(mx);
  95. cv.wait(lock, [this, &pop, &task]
  96. {
  97. pop = tasks.pop(task);
  98. return canceling || stopping || pop;
  99. });
  100. }
  101. if (canceling || (stopping && !pop))
  102. {
  103. return;
  104. }
  105. task();
  106. }
  107. }
  108. inline void ThreadPool::terminate()
  109. {
  110. {
  111. Lock lock(mx);
  112. if (isRunning())
  113. {
  114. stopping = true;
  115. }
  116. else
  117. {
  118. return;
  119. }
  120. }
  121. cv.notify_all();
  122. for (auto& worker : workers)
  123. {
  124. worker.join();
  125. }
  126. }
  127. inline void ThreadPool::cancel()
  128. {
  129. {
  130. Lock lock(mx);
  131. if (running())
  132. {
  133. canceling = true;
  134. }
  135. else
  136. {
  137. return;
  138. }
  139. }
  140. tasks.clear();
  141. cv.notify_all();
  142. for (auto& worker : workers)
  143. {
  144. worker.join();
  145. }
  146. }
  147. auto ThreadPool::async(std::function<void()>&& f) const -> boost::future<void>
  148. {
  149. using TaskT = boost::packaged_task<void>;
  150. {
  151. Lock lock(mx);
  152. if (stopping || canceling)
  153. {
  154. throw std::runtime_error("Delegating task to a threadpool that has been terminated or canceled.");
  155. }
  156. }
  157. auto task = std::make_shared<TaskT>(f);
  158. boost::future<void> fut = task->get_future();
  159. tasks.emplace([task]() -> void
  160. {
  161. (*task)();
  162. });
  163. cv.notify_one();
  164. return fut;
  165. }
  166. VCMI_LIB_NAMESPACE_END