pool.hpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*! \file
  2. * \brief Thread pool core.
  3. *
  4. * This file contains the threadpool's core class: pool<Task, SchedulingPolicy>.
  5. *
  6. * Thread pools are a mechanism for asynchronous and parallel processing
  7. * within the same process. The pool class provides a convenient way
  8. * for dispatching asynchronous tasks as functions objects. The scheduling
  9. * of these tasks can be easily controlled by using customized schedulers.
  10. *
  11. * Copyright (c) 2005-2007 Philipp Henkel
  12. *
  13. * Use, modification, and distribution are subject to the
  14. * Boost Software License, Version 1.0. (See accompanying file
  15. * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  16. *
  17. * http://threadpool.sourceforge.net
  18. *
  19. */
  20. #ifndef THREADPOOL_POOL_HPP_INCLUDED
  21. #define THREADPOOL_POOL_HPP_INCLUDED
  22. #include <boost/ref.hpp>
  23. #include "./detail/pool_core.hpp"
  24. #include "task_adaptors.hpp"
  25. #include "./detail/locking_ptr.hpp"
  26. #include "scheduling_policies.hpp"
  27. #include "size_policies.hpp"
  28. #include "shutdown_policies.hpp"
  29. /// The namespace threadpool contains a thread pool and related utility classes.
  30. namespace boost { namespace threadpool
  31. {
  32. /*! \brief Thread pool.
  33. *
  34. * Thread pools are a mechanism for asynchronous and parallel processing
  35. * within the same process. The pool class provides a convenient way
  36. * for dispatching asynchronous tasks as functions objects. The scheduling
  37. * of these tasks can be easily controlled by using customized schedulers.
  38. * A task must not throw an exception.
  39. *
  40. * A pool is DefaultConstructible, CopyConstructible and Assignable.
  41. * It has reference semantics; all copies of the same pool are equivalent and interchangeable.
  42. * All operations on a pool except assignment are strongly thread safe or sequentially consistent;
  43. * that is, the behavior of concurrent calls is as if the calls have been issued sequentially in an unspecified order.
  44. *
  45. * \param Task A function object which implements the operator 'void operator() (void) const'. The operator () is called by the pool to execute the task. Exceptions are ignored.
  46. * \param SchedulingPolicy A task container which determines how tasks are scheduled. It is guaranteed that this container is accessed only by one thread at a time. The scheduler shall not throw exceptions.
  47. *
  48. * \remarks The pool class is thread-safe.
  49. *
  50. * \see Tasks: task_func, prio_task_func
  51. * \see Scheduling policies: fifo_scheduler, lifo_scheduler, prio_scheduler
  52. */
  53. template <
  54. typename Task = task_func,
  55. template <typename> class SchedulingPolicy = fifo_scheduler,
  56. template <typename> class SizePolicy = static_size,
  57. template <typename> class SizePolicyController = resize_controller,
  58. template <typename> class ShutdownPolicy = wait_for_all_tasks
  59. >
  60. class thread_pool
  61. {
  62. typedef detail::pool_core<Task,
  63. SchedulingPolicy,
  64. SizePolicy,
  65. SizePolicyController,
  66. ShutdownPolicy> pool_core_type;
  67. shared_ptr<pool_core_type> m_core; // pimpl idiom
  68. shared_ptr<void> m_shutdown_controller; // If the last pool holding a pointer to the core is deleted the controller shuts the pool down.
  69. public: // Type definitions
  70. typedef Task task_type; //!< Indicates the task's type.
  71. typedef SchedulingPolicy<task_type> scheduler_type; //!< Indicates the scheduler's type.
  72. /* typedef thread_pool<Task,
  73. SchedulingPolicy,
  74. SizePolicy,
  75. ShutdownPolicy > pool_type; //!< Indicates the thread pool's type.
  76. */
  77. typedef SizePolicy<pool_core_type> size_policy_type;
  78. typedef SizePolicyController<pool_core_type> size_controller_type;
  79. public:
  80. /*! Constructor.
  81. * \param initial_threads The pool is immediately resized to set the specified number of threads. The pool's actual number threads depends on the SizePolicy.
  82. */
  83. thread_pool(size_t initial_threads = 0)
  84. : m_core(new pool_core_type)
  85. , m_shutdown_controller(static_cast<void*>(0), bind(&pool_core_type::shutdown, m_core))
  86. {
  87. size_policy_type::init(*m_core, initial_threads);
  88. }
  89. /*! Gets the size controller which manages the number of threads in the pool.
  90. * \return The size controller.
  91. * \see SizePolicy
  92. */
  93. size_controller_type size_controller()
  94. {
  95. return m_core->size_controller();
  96. }
  97. /*! Gets the number of threads in the pool.
  98. * \return The number of threads.
  99. */
  100. size_t size() const
  101. {
  102. return m_core->size();
  103. }
  104. /*! Schedules a task for asynchronous execution. The task will be executed once only.
  105. * \param task The task function object. It should not throw execeptions.
  106. * \return true, if the task could be scheduled and false otherwise.
  107. */
  108. bool schedule(task_type const & task)
  109. {
  110. return m_core->schedule(task);
  111. }
  112. /*! Returns the number of tasks which are currently executed.
  113. * \return The number of active tasks.
  114. */
  115. size_t active() const
  116. {
  117. return m_core->active();
  118. }
  119. /*! Returns the number of tasks which are ready for execution.
  120. * \return The number of pending tasks.
  121. */
  122. size_t pending() const
  123. {
  124. return m_core->pending();
  125. }
  126. /*! Removes all pending tasks from the pool's scheduler.
  127. */
  128. void clear()
  129. {
  130. m_core->clear();
  131. }
  132. /*! Indicates that there are no tasks pending.
  133. * \return true if there are no tasks ready for execution.
  134. * \remarks This function is more efficient that the check 'pending() == 0'.
  135. */
  136. bool empty() const
  137. {
  138. return m_core->empty();
  139. }
  140. /*! The current thread of execution is blocked until the sum of all active
  141. * and pending tasks is equal or less than a given threshold.
  142. * \param task_threshold The maximum number of tasks in pool and scheduler.
  143. */
  144. void wait(size_t task_threshold = 0) const
  145. {
  146. m_core->wait(task_threshold);
  147. }
  148. /*! The current thread of execution is blocked until the timestamp is met
  149. * or the sum of all active and pending tasks is equal or less
  150. * than a given threshold.
  151. * \param timestamp The time when function returns at the latest.
  152. * \param task_threshold The maximum number of tasks in pool and scheduler.
  153. * \return true if the task sum is equal or less than the threshold, false otherwise.
  154. */
  155. bool wait(xtime const & timestamp, size_t task_threshold = 0) const
  156. {
  157. return m_core->wait(timestamp, task_threshold);
  158. }
  159. };
  160. /*! \brief Fifo pool.
  161. *
  162. * The pool's tasks are fifo scheduled task_func functors.
  163. *
  164. */
  165. typedef thread_pool<task_func, fifo_scheduler, static_size, resize_controller, wait_for_all_tasks> fifo_pool;
  166. /*! \brief Lifo pool.
  167. *
  168. * The pool's tasks are lifo scheduled task_func functors.
  169. *
  170. */
  171. typedef thread_pool<task_func, lifo_scheduler, static_size, resize_controller, wait_for_all_tasks> lifo_pool;
  172. /*! \brief Pool for prioritized task.
  173. *
  174. * The pool's tasks are prioritized prio_task_func functors.
  175. *
  176. */
  177. typedef thread_pool<prio_task_func, prio_scheduler, static_size, resize_controller, wait_for_all_tasks> prio_pool;
  178. /*! \brief A standard pool.
  179. *
  180. * The pool's tasks are fifo scheduled task_func functors.
  181. *
  182. */
  183. typedef fifo_pool pool;
  184. } } // namespace boost::threadpool
  185. #endif // THREADPOOL_POOL_HPP_INCLUDED