Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:28:45

0001 //
0002 // detail/thread_group.hpp
0003 // ~~~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2023 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_DETAIL_THREAD_GROUP_HPP
0012 #define BOOST_ASIO_DETAIL_THREAD_GROUP_HPP
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 <boost/asio/detail/scoped_ptr.hpp>
0020 #include <boost/asio/detail/thread.hpp>
0021 
0022 #include <boost/asio/detail/push_options.hpp>
0023 
0024 namespace boost {
0025 namespace asio {
0026 namespace detail {
0027 
0028 class thread_group
0029 {
0030 public:
0031   // Constructor initialises an empty thread group.
0032   thread_group()
0033     : first_(0)
0034   {
0035   }
0036 
0037   // Destructor joins any remaining threads in the group.
0038   ~thread_group()
0039   {
0040     join();
0041   }
0042 
0043   // Create a new thread in the group.
0044   template <typename Function>
0045   void create_thread(Function f)
0046   {
0047     first_ = new item(f, first_);
0048   }
0049 
0050   // Create new threads in the group.
0051   template <typename Function>
0052   void create_threads(Function f, std::size_t num_threads)
0053   {
0054     for (std::size_t i = 0; i < num_threads; ++i)
0055       create_thread(f);
0056   }
0057 
0058   // Wait for all threads in the group to exit.
0059   void join()
0060   {
0061     while (first_)
0062     {
0063       first_->thread_.join();
0064       item* tmp = first_;
0065       first_ = first_->next_;
0066       delete tmp;
0067     }
0068   }
0069 
0070   // Test whether the group is empty.
0071   bool empty() const
0072   {
0073     return first_ == 0;
0074   }
0075 
0076 private:
0077   // Structure used to track a single thread in the group.
0078   struct item
0079   {
0080     template <typename Function>
0081     explicit item(Function f, item* next)
0082       : thread_(f),
0083         next_(next)
0084     {
0085     }
0086 
0087     boost::asio::detail::thread thread_;
0088     item* next_;
0089   };
0090 
0091   // The first thread in the group.
0092   item* first_;
0093 };
0094 
0095 } // namespace detail
0096 } // namespace asio
0097 } // namespace boost
0098 
0099 #include <boost/asio/detail/pop_options.hpp>
0100 
0101 #endif // BOOST_ASIO_DETAIL_THREAD_GROUP_HPP