thread_pool.hpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. //
  2. // thread_pool.hpp
  3. // ~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 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 io_context, 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. // The underlying scheduler.
  91. detail::scheduler& scheduler_;
  92. // The threads in the pool.
  93. detail::thread_group threads_;
  94. };
  95. /// Executor used to submit functions to a thread pool.
  96. class thread_pool::executor_type
  97. {
  98. public:
  99. /// Obtain the underlying execution context.
  100. thread_pool& context() const BOOST_ASIO_NOEXCEPT;
  101. /// Inform the thread pool that it has some outstanding work to do.
  102. /**
  103. * This function is used to inform the thread pool that some work has begun.
  104. * This ensures that the thread pool's join() function will not return while
  105. * the work is underway.
  106. */
  107. void on_work_started() const BOOST_ASIO_NOEXCEPT;
  108. /// Inform the thread pool that some work is no longer outstanding.
  109. /**
  110. * This function is used to inform the thread pool that some work has
  111. * finished. Once the count of unfinished work reaches zero, the thread
  112. * pool's join() function is permitted to exit.
  113. */
  114. void on_work_finished() const BOOST_ASIO_NOEXCEPT;
  115. /// Request the thread pool to invoke the given function object.
  116. /**
  117. * This function is used to ask the thread pool to execute the given function
  118. * object. If the current thread belongs to the pool, @c dispatch() executes
  119. * the function before returning. Otherwise, the function will be scheduled
  120. * to run on the thread pool.
  121. *
  122. * @param f The function object to be called. The executor will make
  123. * a copy of the handler object as required. The function signature of the
  124. * function object must be: @code void function(); @endcode
  125. *
  126. * @param a An allocator that may be used by the executor to allocate the
  127. * internal storage needed for function invocation.
  128. */
  129. template <typename Function, typename Allocator>
  130. void dispatch(BOOST_ASIO_MOVE_ARG(Function) f, const Allocator& a) const;
  131. /// Request the thread pool to invoke the given function object.
  132. /**
  133. * This function is used to ask the thread pool to execute the given function
  134. * object. The function object will never be executed inside @c post().
  135. * Instead, it will be scheduled to run on the thread pool.
  136. *
  137. * @param f The function object to be called. The executor will make
  138. * a copy of the handler object as required. The function signature of the
  139. * function object must be: @code void function(); @endcode
  140. *
  141. * @param a An allocator that may be used by the executor to allocate the
  142. * internal storage needed for function invocation.
  143. */
  144. template <typename Function, typename Allocator>
  145. void post(BOOST_ASIO_MOVE_ARG(Function) f, const Allocator& a) const;
  146. /// Request the thread pool to invoke the given function object.
  147. /**
  148. * This function is used to ask the thread pool to execute the given function
  149. * object. The function object will never be executed inside @c defer().
  150. * Instead, it will be scheduled to run on the thread pool.
  151. *
  152. * If the current thread belongs to the thread pool, @c defer() will delay
  153. * scheduling the function object until the current thread returns control to
  154. * the pool.
  155. *
  156. * @param f The function object to be called. The executor will make
  157. * a copy of the handler object as required. The function signature of the
  158. * function object must be: @code void function(); @endcode
  159. *
  160. * @param a An allocator that may be used by the executor to allocate the
  161. * internal storage needed for function invocation.
  162. */
  163. template <typename Function, typename Allocator>
  164. void defer(BOOST_ASIO_MOVE_ARG(Function) f, const Allocator& a) const;
  165. /// Determine whether the thread pool is running in the current thread.
  166. /**
  167. * @return @c true if the current thread belongs to the pool. Otherwise
  168. * returns @c false.
  169. */
  170. bool running_in_this_thread() const BOOST_ASIO_NOEXCEPT;
  171. /// Compare two executors for equality.
  172. /**
  173. * Two executors are equal if they refer to the same underlying thread pool.
  174. */
  175. friend bool operator==(const executor_type& a,
  176. const executor_type& b) BOOST_ASIO_NOEXCEPT
  177. {
  178. return &a.pool_ == &b.pool_;
  179. }
  180. /// Compare two executors for inequality.
  181. /**
  182. * Two executors are equal if they refer to the same underlying thread pool.
  183. */
  184. friend bool operator!=(const executor_type& a,
  185. const executor_type& b) BOOST_ASIO_NOEXCEPT
  186. {
  187. return &a.pool_ != &b.pool_;
  188. }
  189. private:
  190. friend class thread_pool;
  191. // Constructor.
  192. explicit executor_type(thread_pool& p) : pool_(p) {}
  193. // The underlying thread pool.
  194. thread_pool& pool_;
  195. };
  196. } // namespace asio
  197. } // namespace boost
  198. #include <boost/asio/detail/pop_options.hpp>
  199. #include <boost/asio/impl/thread_pool.hpp>
  200. #if defined(BOOST_ASIO_HEADER_ONLY)
  201. # include <boost/asio/impl/thread_pool.ipp>
  202. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  203. #endif // BOOST_ASIO_THREAD_POOL_HPP