2 // impl/thread_pool.ipp
3 // ~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_IMPL_THREAD_POOL_IPP
12 #define BOOST_ASIO_IMPL_THREAD_POOL_IPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
19 #include <boost/asio/thread_pool.hpp>
21 #include <boost/asio/detail/push_options.hpp>
26 struct thread_pool::thread_function
28 detail::scheduler* scheduler_;
32 boost::system::error_code ec;
37 thread_pool::thread_pool()
38 : scheduler_(add_scheduler(new detail::scheduler(*this, 0, false)))
40 scheduler_.work_started();
42 thread_function f = { &scheduler_ };
43 std::size_t num_threads = detail::thread::hardware_concurrency() * 2;
44 threads_.create_threads(f, num_threads ? num_threads : 2);
47 thread_pool::thread_pool(std::size_t num_threads)
48 : scheduler_(add_scheduler(new detail::scheduler(
49 *this, num_threads == 1 ? 1 : 0, false)))
51 scheduler_.work_started();
53 thread_function f = { &scheduler_ };
54 threads_.create_threads(f, num_threads);
57 thread_pool::~thread_pool()
63 void thread_pool::stop()
68 void thread_pool::join()
70 if (!threads_.empty())
72 scheduler_.work_finished();
77 detail::scheduler& thread_pool::add_scheduler(detail::scheduler* s)
79 detail::scoped_ptr<detail::scheduler> scoped_impl(s);
80 boost::asio::add_service<detail::scheduler>(*this, scoped_impl.get());
81 return *scoped_impl.release();
87 #include <boost/asio/detail/pop_options.hpp>
89 #endif // BOOST_ASIO_IMPL_THREAD_POOL_IPP