2 // detail/thread_group.hpp
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_DETAIL_THREAD_GROUP_HPP
12 #define BOOST_ASIO_DETAIL_THREAD_GROUP_HPP
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/detail/scoped_ptr.hpp>
20 #include <boost/asio/detail/thread.hpp>
29 // Constructor initialises an empty thread group.
35 // Destructor joins any remaining threads in the group.
41 // Create a new thread in the group.
42 template <typename Function>
43 void create_thread(Function f)
45 first_ = new item(f, first_);
48 // Create new threads in the group.
49 template <typename Function>
50 void create_threads(Function f, std::size_t num_threads)
52 for (std::size_t i = 0; i < num_threads; ++i)
56 // Wait for all threads in the group to exit.
61 first_->thread_.join();
63 first_ = first_->next_;
68 // Test whether the group is empty.
75 // Structure used to track a single thread in the group.
78 template <typename Function>
79 explicit item(Function f, item* next)
85 boost::asio::detail::thread thread_;
89 // The first thread in the group.
97 #endif // BOOST_ASIO_DETAIL_THREAD_GROUP_HPP