2 // impl/system_context.ipp
3 // ~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2022 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_SYSTEM_CONTEXT_IPP
12 #define BOOST_ASIO_IMPL_SYSTEM_CONTEXT_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/system_context.hpp>
21 #include <boost/asio/detail/push_options.hpp>
26 struct system_context::thread_function
28 detail::scheduler* scheduler_;
32 #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
35 #endif// !defined(BOOST_ASIO_NO_EXCEPTIONS)
36 boost::system::error_code ec;
38 #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
44 #endif// !defined(BOOST_ASIO_NO_EXCEPTIONS)
48 system_context::system_context()
49 : scheduler_(add_scheduler(new detail::scheduler(*this, 0, false)))
51 scheduler_.work_started();
53 thread_function f = { &scheduler_ };
54 num_threads_ = detail::thread::hardware_concurrency() * 2;
55 num_threads_ = num_threads_ ? num_threads_ : 2;
56 threads_.create_threads(f, num_threads_);
59 system_context::~system_context()
61 scheduler_.work_finished();
66 void system_context::stop()
71 bool system_context::stopped() const BOOST_ASIO_NOEXCEPT
73 return scheduler_.stopped();
76 void system_context::join()
78 scheduler_.work_finished();
82 detail::scheduler& system_context::add_scheduler(detail::scheduler* s)
84 detail::scoped_ptr<detail::scheduler> scoped_impl(s);
85 boost::asio::add_service<detail::scheduler>(*this, scoped_impl.get());
86 return *scoped_impl.release();
92 #include <boost/asio/detail/pop_options.hpp>
94 #endif // BOOST_ASIO_IMPL_SYSTEM_CONTEXT_IPP