thread_pool.hpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. //
  2. // thread_pool.hpp
  3. // ~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_ASIO_THREAD_POOL_HPP
  11. #define BOOST_ASIO_THREAD_POOL_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #include <boost/asio/detail/noncopyable.hpp>
  17. #include <boost/asio/detail/scheduler.hpp>
  18. #include <boost/asio/detail/thread_group.hpp>
  19. #include <boost/asio/execution_context.hpp>
  20. #include <boost/asio/detail/push_options.hpp>
  21. namespace boost {
  22. namespace asio {
  23. /// A simple fixed-size thread pool.
  24. /**
  25. * The thread pool class is an execution context where functions are permitted
  26. * to run on one of a fixed number of threads.
  27. *
  28. * @par Submitting tasks to the pool
  29. *
  30. * To submit functions to the thread_pool, use the @ref boost::asio::dispatch,
  31. * @ref boost::asio::post or @ref boost::asio::defer free functions.
  32. *
  33. * For example:
  34. *
  35. * @code void my_task()
  36. * {
  37. * ...
  38. * }
  39. *
  40. * ...
  41. *
  42. * // Launch the pool with four threads.
  43. * boost::asio::thread_pool pool(4);
  44. *
  45. * // Submit a function to the pool.
  46. * boost::asio::post(pool, my_task);
  47. *
  48. * // Submit a lambda object to the pool.
  49. * boost::asio::post(pool,
  50. * []()
  51. * {
  52. * ...
  53. * });
  54. *
  55. * // Wait for all tasks in the pool to complete.
  56. * pool.join(); @endcode
  57. */
  58. class thread_pool
  59. : public execution_context
  60. {
  61. public:
  62. class executor_type;
  63. /// Constructs a pool with an automatically determined number of threads.
  64. BOOST_ASIO_DECL thread_pool();
  65. /// Constructs a pool with a specified number of threads.
  66. BOOST_ASIO_DECL thread_pool(std::size_t num_threads);
  67. /// Destructor.
  68. /**
  69. * Automatically stops and joins the pool, if not explicitly done beforehand.
  70. */
  71. BOOST_ASIO_DECL ~thread_pool();
  72. /// Obtains the executor associated with the pool.
  73. executor_type get_executor() BOOST_ASIO_NOEXCEPT;
  74. /// Stops the threads.
  75. /**
  76. * This function stops the threads as soon as possible. As a result of calling
  77. * @c stop(), pending function objects may be never be invoked.
  78. */
  79. BOOST_ASIO_DECL void stop();
  80. /// Joins the threads.
  81. /**
  82. * This function blocks until the threads in the pool have completed. If @c
  83. * stop() is not called prior to @c join(), the @c join() call will wait
  84. * until the pool has no more outstanding work.
  85. */
  86. BOOST_ASIO_DECL void join();
  87. private:
  88. friend class executor_type;
  89. struct thread_function;
  90. // Helper function to create the underlying scheduler.
  91. BOOST_ASIO_DECL detail::scheduler& add_scheduler(detail::scheduler* s);
  92. // The underlying scheduler.
  93. detail::scheduler& scheduler_;
  94. // The threads in the pool.
  95. detail::thread_group threads_;
  96. };
  97. /// Executor used to submit functions to a thread pool.
  98. class thread_pool::executor_type
  99. {
  100. public:
  101. /// Obtain the underlying execution context.
  102. thread_pool& context() const BOOST_ASIO_NOEXCEPT;
  103. /// Inform the thread pool that it has some outstanding work to do.
  104. /**
  105. * This function is used to inform the thread pool that some work has begun.
  106. * This ensures that the thread pool's join() function will not return while
  107. * the work is underway.
  108. */
  109. void on_work_started() const BOOST_ASIO_NOEXCEPT;
  110. /// Inform the thread pool that some work is no longer outstanding.
  111. /**
  112. * This function is used to inform the thread pool that some work has
  113. * finished. Once the count of unfinished work reaches zero, the thread
  114. * pool's join() function is permitted to exit.
  115. */
  116. void on_work_finished() const BOOST_ASIO_NOEXCEPT;
  117. /// Request the thread pool to invoke the given function object.
  118. /**
  119. * This function is used to ask the thread pool to execute the given function
  120. * object. If the current thread belongs to the pool, @c dispatch() executes
  121. * the function before returning. Otherwise, the function will be scheduled
  122. * to run on the thread pool.
  123. *
  124. * @param f The function object to be called. The executor will make
  125. * a copy of the handler object as required. The function signature of the
  126. * function object must be: @code void function(); @endcode
  127. *
  128. * @param a An allocator that may be used by the executor to allocate the
  129. * internal storage needed for function invocation.
  130. */
  131. template <typename Function, typename Allocator>
  132. void dispatch(BOOST_ASIO_MOVE_ARG(Function) f, const Allocator& a) const;
  133. /// Request the thread pool to invoke the given function object.
  134. /**
  135. * This function is used to ask the thread pool to execute the given function
  136. * object. The function object will never be executed inside @c post().
  137. * Instead, it will be scheduled to run on the thread pool.
  138. *
  139. * @param f The function object to be called. The executor will make
  140. * a copy of the handler object as required. The function signature of the
  141. * function object must be: @code void function(); @endcode
  142. *
  143. * @param a An allocator that may be used by the executor to allocate the
  144. * internal storage needed for function invocation.
  145. */
  146. template <typename Function, typename Allocator>
  147. void post(BOOST_ASIO_MOVE_ARG(Function) f, const Allocator& a) const;
  148. /// Request the thread pool to invoke the given function object.
  149. /**
  150. * This function is used to ask the thread pool to execute the given function
  151. * object. The function object will never be executed inside @c defer().
  152. * Instead, it will be scheduled to run on the thread pool.
  153. *
  154. * If the current thread belongs to the thread pool, @c defer() will delay
  155. * scheduling the function object until the current thread returns control to
  156. * the pool.
  157. *
  158. * @param f The function object to be called. The executor will make
  159. * a copy of the handler object as required. The function signature of the
  160. * function object must be: @code void function(); @endcode
  161. *
  162. * @param a An allocator that may be used by the executor to allocate the
  163. * internal storage needed for function invocation.
  164. */
  165. template <typename Function, typename Allocator>
  166. void defer(BOOST_ASIO_MOVE_ARG(Function) f, const Allocator& a) const;
  167. /// Determine whether the thread pool is running in the current thread.
  168. /**
  169. * @return @c true if the current thread belongs to the pool. Otherwise
  170. * returns @c false.
  171. */
  172. bool running_in_this_thread() const BOOST_ASIO_NOEXCEPT;
  173. /// Compare two executors for equality.
  174. /**
  175. * Two executors are equal if they refer to the same underlying thread pool.
  176. */
  177. friend bool operator==(const executor_type& a,
  178. const executor_type& b) BOOST_ASIO_NOEXCEPT
  179. {
  180. return &a.pool_ == &b.pool_;
  181. }
  182. /// Compare two executors for inequality.
  183. /**
  184. * Two executors are equal if they refer to the same underlying thread pool.
  185. */
  186. friend bool operator!=(const executor_type& a,
  187. const executor_type& b) BOOST_ASIO_NOEXCEPT
  188. {
  189. return &a.pool_ != &b.pool_;
  190. }
  191. private:
  192. friend class thread_pool;
  193. // Constructor.
  194. explicit executor_type(thread_pool& p) : pool_(p) {}
  195. // The underlying thread pool.
  196. thread_pool& pool_;
  197. };
  198. } // namespace asio
  199. } // namespace boost
  200. #include <boost/asio/detail/pop_options.hpp>
  201. #include <boost/asio/impl/thread_pool.hpp>
  202. #if defined(BOOST_ASIO_HEADER_ONLY)
  203. # include <boost/asio/impl/thread_pool.ipp>
  204. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  205. #endif // BOOST_ASIO_THREAD_POOL_HPP