File indexing completed on 2025-09-17 08:23:34
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef BOOST_ASIO_IMPL_THREAD_POOL_IPP
0012 #define BOOST_ASIO_IMPL_THREAD_POOL_IPP
0013
0014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0015 # pragma once
0016 #endif
0017
0018 #include <boost/asio/detail/config.hpp>
0019 #include <stdexcept>
0020 #include <boost/asio/thread_pool.hpp>
0021 #include <boost/asio/detail/throw_exception.hpp>
0022
0023 #include <boost/asio/detail/push_options.hpp>
0024
0025 namespace boost {
0026 namespace asio {
0027
0028 struct thread_pool::thread_function
0029 {
0030 detail::scheduler* scheduler_;
0031
0032 void operator()()
0033 {
0034 #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
0035 try
0036 {
0037 #endif
0038 boost::system::error_code ec;
0039 scheduler_->run(ec);
0040 #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
0041 }
0042 catch (...)
0043 {
0044 std::terminate();
0045 }
0046 #endif
0047 }
0048 };
0049
0050 #if !defined(BOOST_ASIO_NO_TS_EXECUTORS)
0051 namespace detail {
0052
0053 inline long default_thread_pool_size()
0054 {
0055 std::size_t num_threads = thread::hardware_concurrency() * 2;
0056 num_threads = num_threads == 0 ? 2 : num_threads;
0057 return static_cast<long>(num_threads);
0058 }
0059
0060 }
0061
0062 thread_pool::thread_pool()
0063 : scheduler_(add_scheduler(new detail::scheduler(*this, false))),
0064 num_threads_(detail::default_thread_pool_size()),
0065 joinable_(true)
0066 {
0067 scheduler_.work_started();
0068
0069 thread_function f = { &scheduler_ };
0070 threads_.create_threads(f, static_cast<std::size_t>(num_threads_));
0071 }
0072 #endif
0073
0074 namespace detail {
0075
0076 inline long clamp_thread_pool_size(std::size_t n)
0077 {
0078 if (n > 0x7FFFFFFF)
0079 {
0080 std::out_of_range ex("thread pool size");
0081 boost::asio::detail::throw_exception(ex);
0082 }
0083 return static_cast<long>(n & 0x7FFFFFFF);
0084 }
0085
0086 }
0087
0088 thread_pool::thread_pool(std::size_t num_threads)
0089 : execution_context(config_from_concurrency_hint(num_threads == 1 ? 1 : 0)),
0090 scheduler_(add_scheduler(new detail::scheduler(*this, false))),
0091 num_threads_(detail::clamp_thread_pool_size(num_threads)),
0092 joinable_(true)
0093 {
0094 scheduler_.work_started();
0095
0096 thread_function f = { &scheduler_ };
0097 threads_.create_threads(f, static_cast<std::size_t>(num_threads_));
0098 }
0099
0100 thread_pool::thread_pool(std::size_t num_threads,
0101 const execution_context::service_maker& initial_services)
0102 : execution_context(initial_services),
0103 scheduler_(add_scheduler(new detail::scheduler(*this, false))),
0104 num_threads_(detail::clamp_thread_pool_size(num_threads)),
0105 joinable_(true)
0106 {
0107 scheduler_.work_started();
0108
0109 thread_function f = { &scheduler_ };
0110 threads_.create_threads(f, static_cast<std::size_t>(num_threads_));
0111 }
0112
0113 thread_pool::~thread_pool()
0114 {
0115 stop();
0116 join();
0117 shutdown();
0118 }
0119
0120 void thread_pool::stop()
0121 {
0122 scheduler_.stop();
0123 }
0124
0125 void thread_pool::attach()
0126 {
0127 ++num_threads_;
0128 thread_function f = { &scheduler_ };
0129 f();
0130 }
0131
0132 void thread_pool::join()
0133 {
0134 if (joinable_)
0135 {
0136 joinable_ = false;
0137 scheduler_.work_finished();
0138 threads_.join();
0139 }
0140 }
0141
0142 detail::scheduler& thread_pool::add_scheduler(detail::scheduler* s)
0143 {
0144 detail::scoped_ptr<detail::scheduler> scoped_impl(s);
0145 boost::asio::add_service<detail::scheduler>(*this, scoped_impl.get());
0146 return *scoped_impl.release();
0147 }
0148
0149 void thread_pool::wait()
0150 {
0151 join();
0152 }
0153
0154 }
0155 }
0156
0157 #include <boost/asio/detail/pop_options.hpp>
0158
0159 #endif