thread_pool.ipp 2.9 KB

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