thread_pool.ipp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // impl/thread_pool.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 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_IMPL_THREAD_POOL_IPP
  11. #define BOOST_ASIO_IMPL_THREAD_POOL_IPP
  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 <stdexcept>
  17. #include <boost/asio/thread_pool.hpp>
  18. #include <boost/asio/detail/throw_exception.hpp>
  19. #include <boost/asio/detail/push_options.hpp>
  20. namespace boost {
  21. namespace asio {
  22. struct thread_pool::thread_function
  23. {
  24. detail::scheduler* scheduler_;
  25. void operator()()
  26. {
  27. #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
  28. try
  29. {
  30. #endif// !defined(BOOST_ASIO_NO_EXCEPTIONS)
  31. boost::system::error_code ec;
  32. scheduler_->run(ec);
  33. #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
  34. }
  35. catch (...)
  36. {
  37. std::terminate();
  38. }
  39. #endif// !defined(BOOST_ASIO_NO_EXCEPTIONS)
  40. }
  41. };
  42. #if !defined(BOOST_ASIO_NO_TS_EXECUTORS)
  43. namespace detail {
  44. inline long default_thread_pool_size()
  45. {
  46. std::size_t num_threads = thread::hardware_concurrency() * 2;
  47. num_threads = num_threads == 0 ? 2 : num_threads;
  48. return static_cast<long>(num_threads);
  49. }
  50. } // namespace detail
  51. thread_pool::thread_pool()
  52. : scheduler_(add_scheduler(new detail::scheduler(*this, 0, false))),
  53. num_threads_(detail::default_thread_pool_size())
  54. {
  55. scheduler_.work_started();
  56. thread_function f = { &scheduler_ };
  57. threads_.create_threads(f, static_cast<std::size_t>(num_threads_));
  58. }
  59. #endif // !defined(BOOST_ASIO_NO_TS_EXECUTORS)
  60. namespace detail {
  61. inline long clamp_thread_pool_size(std::size_t n)
  62. {
  63. if (n > 0x7FFFFFFF)
  64. {
  65. std::out_of_range ex("thread pool size");
  66. boost::asio::detail::throw_exception(ex);
  67. }
  68. return static_cast<long>(n & 0x7FFFFFFF);
  69. }
  70. } // namespace detail
  71. thread_pool::thread_pool(std::size_t num_threads)
  72. : scheduler_(add_scheduler(new detail::scheduler(
  73. *this, num_threads == 1 ? 1 : 0, false))),
  74. num_threads_(detail::clamp_thread_pool_size(num_threads))
  75. {
  76. scheduler_.work_started();
  77. thread_function f = { &scheduler_ };
  78. threads_.create_threads(f, static_cast<std::size_t>(num_threads_));
  79. }
  80. thread_pool::~thread_pool()
  81. {
  82. stop();
  83. join();
  84. shutdown();
  85. }
  86. void thread_pool::stop()
  87. {
  88. scheduler_.stop();
  89. }
  90. void thread_pool::attach()
  91. {
  92. ++num_threads_;
  93. thread_function f = { &scheduler_ };
  94. f();
  95. }
  96. void thread_pool::join()
  97. {
  98. if (num_threads_)
  99. scheduler_.work_finished();
  100. if (!threads_.empty())
  101. threads_.join();
  102. }
  103. detail::scheduler& thread_pool::add_scheduler(detail::scheduler* s)
  104. {
  105. detail::scoped_ptr<detail::scheduler> scoped_impl(s);
  106. boost::asio::add_service<detail::scheduler>(*this, scoped_impl.get());
  107. return *scoped_impl.release();
  108. }
  109. void thread_pool::wait()
  110. {
  111. scheduler_.work_finished();
  112. threads_.join();
  113. }
  114. } // namespace asio
  115. } // namespace boost
  116. #include <boost/asio/detail/pop_options.hpp>
  117. #endif // BOOST_ASIO_IMPL_THREAD_POOL_IPP