Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/boost/asio/impl/thread_pool.ipp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 //
0002 // impl/thread_pool.ipp
0003 // ~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2025 Christopher M. Kohlhoff (chris at kohlhoff dot com)
0006 //
0007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
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 // defined(_MSC_VER) && (_MSC_VER >= 1200)
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// !defined(BOOST_ASIO_NO_EXCEPTIONS)
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// !defined(BOOST_ASIO_NO_EXCEPTIONS)
0047   }
0048 };
0049 
0050 #if !defined(BOOST_ASIO_NO_TS_EXECUTORS)
0051 
0052 long thread_pool::default_thread_pool_size()
0053 {
0054   std::size_t num_threads = detail::thread::hardware_concurrency() * 2;
0055   num_threads = num_threads == 0 ? 2 : num_threads;
0056   return static_cast<long>(num_threads);
0057 }
0058 
0059 thread_pool::thread_pool()
0060   : scheduler_(boost::asio::make_service<detail::scheduler>(*this, false)),
0061     threads_(allocator<void>(*this)),
0062     num_threads_(default_thread_pool_size()),
0063     joinable_(true)
0064 {
0065   start();
0066 }
0067 
0068 #endif // !defined(BOOST_ASIO_NO_TS_EXECUTORS)
0069 
0070 long thread_pool::clamp_thread_pool_size(std::size_t n)
0071 {
0072   if (n > 0x7FFFFFFF)
0073   {
0074     std::out_of_range ex("thread pool size");
0075     boost::asio::detail::throw_exception(ex);
0076   }
0077   return static_cast<long>(n & 0x7FFFFFFF);
0078 }
0079 
0080 thread_pool::thread_pool(std::size_t num_threads)
0081   : execution_context(config_from_concurrency_hint(num_threads == 1 ? 1 : 0)),
0082     scheduler_(boost::asio::make_service<detail::scheduler>(*this, false)),
0083     threads_(allocator<void>(*this)),
0084     num_threads_(clamp_thread_pool_size(num_threads)),
0085     joinable_(true)
0086 {
0087   start();
0088 }
0089 
0090 thread_pool::thread_pool(std::size_t num_threads,
0091     const execution_context::service_maker& initial_services)
0092   : execution_context(initial_services),
0093     scheduler_(boost::asio::make_service<detail::scheduler>(*this, false)),
0094     threads_(allocator<void>(*this)),
0095     num_threads_(clamp_thread_pool_size(num_threads)),
0096     joinable_(true)
0097 {
0098   start();
0099 }
0100 
0101 thread_pool::~thread_pool()
0102 {
0103   stop();
0104   join();
0105   shutdown();
0106 }
0107 
0108 void thread_pool::start()
0109 {
0110   scheduler_.work_started();
0111   thread_function f = { &scheduler_ };
0112   threads_.create_threads(f, static_cast<std::size_t>(num_threads_));
0113 }
0114 
0115 void thread_pool::stop()
0116 {
0117   scheduler_.stop();
0118 }
0119 
0120 void thread_pool::attach()
0121 {
0122   ++num_threads_;
0123   thread_function f = { &scheduler_ };
0124   f();
0125 }
0126 
0127 void thread_pool::join()
0128 {
0129   if (joinable_)
0130   {
0131     joinable_ = false;
0132     scheduler_.work_finished();
0133     threads_.join();
0134   }
0135 }
0136 
0137 void thread_pool::wait()
0138 {
0139   join();
0140 }
0141 
0142 } // namespace asio
0143 } // namespace boost
0144 
0145 #include <boost/asio/detail/pop_options.hpp>
0146 
0147 #endif // BOOST_ASIO_IMPL_THREAD_POOL_IPP