File indexing completed on 2025-01-18 09:52:47
0001 #ifndef BOOST_THREAD_DETAIL_THREAD_GROUP_HPP
0002 #define BOOST_THREAD_DETAIL_THREAD_GROUP_HPP
0003
0004
0005
0006
0007
0008 #include <list>
0009 #include <boost/thread/csbl/memory/unique_ptr.hpp>
0010 #include <boost/thread/shared_mutex.hpp>
0011 #include <boost/thread/mutex.hpp>
0012 #include <boost/thread/lock_guard.hpp>
0013
0014 #include <boost/config/abi_prefix.hpp>
0015
0016 #ifdef BOOST_MSVC
0017 #pragma warning(push)
0018 #pragma warning(disable:4251)
0019 #endif
0020
0021 namespace boost
0022 {
0023 class thread_group
0024 {
0025 private:
0026 thread_group(thread_group const&);
0027 thread_group& operator=(thread_group const&);
0028 public:
0029 thread_group() {}
0030 ~thread_group()
0031 {
0032 for(std::list<thread*>::iterator it=threads.begin(),end=threads.end();
0033 it!=end;
0034 ++it)
0035 {
0036 delete *it;
0037 }
0038 }
0039
0040 bool is_this_thread_in()
0041 {
0042 thread::id id = this_thread::get_id();
0043 boost::shared_lock<shared_mutex> guard(m);
0044 for(std::list<thread*>::iterator it=threads.begin(),end=threads.end();
0045 it!=end;
0046 ++it)
0047 {
0048 if ((*it)->get_id() == id)
0049 return true;
0050 }
0051 return false;
0052 }
0053
0054 bool is_thread_in(thread* thrd)
0055 {
0056 if(thrd)
0057 {
0058 thread::id id = thrd->get_id();
0059 boost::shared_lock<shared_mutex> guard(m);
0060 for(std::list<thread*>::iterator it=threads.begin(),end=threads.end();
0061 it!=end;
0062 ++it)
0063 {
0064 if ((*it)->get_id() == id)
0065 return true;
0066 }
0067 return false;
0068 }
0069 else
0070 {
0071 return false;
0072 }
0073 }
0074
0075 template<typename F>
0076 thread* create_thread(F threadfunc)
0077 {
0078 boost::lock_guard<shared_mutex> guard(m);
0079 boost::csbl::unique_ptr<thread> new_thread(new thread(threadfunc));
0080 threads.push_back(new_thread.get());
0081 return new_thread.release();
0082 }
0083
0084 void add_thread(thread* thrd)
0085 {
0086 if(thrd)
0087 {
0088 BOOST_THREAD_ASSERT_PRECONDITION( ! is_thread_in(thrd) ,
0089 thread_resource_error(static_cast<int>(system::errc::resource_deadlock_would_occur), "boost::thread_group: trying to add a duplicated thread")
0090 );
0091
0092 boost::lock_guard<shared_mutex> guard(m);
0093 threads.push_back(thrd);
0094 }
0095 }
0096
0097 void remove_thread(thread* thrd)
0098 {
0099 boost::lock_guard<shared_mutex> guard(m);
0100 std::list<thread*>::iterator const it=std::find(threads.begin(),threads.end(),thrd);
0101 if(it!=threads.end())
0102 {
0103 threads.erase(it);
0104 }
0105 }
0106
0107 void join_all()
0108 {
0109 BOOST_THREAD_ASSERT_PRECONDITION( ! is_this_thread_in() ,
0110 thread_resource_error(static_cast<int>(system::errc::resource_deadlock_would_occur), "boost::thread_group: trying joining itself")
0111 );
0112 boost::shared_lock<shared_mutex> guard(m);
0113
0114 for(std::list<thread*>::iterator it=threads.begin(),end=threads.end();
0115 it!=end;
0116 ++it)
0117 {
0118 if ((*it)->joinable())
0119 (*it)->join();
0120 }
0121 }
0122
0123 #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
0124 void interrupt_all()
0125 {
0126 boost::shared_lock<shared_mutex> guard(m);
0127
0128 for(std::list<thread*>::iterator it=threads.begin(),end=threads.end();
0129 it!=end;
0130 ++it)
0131 {
0132 (*it)->interrupt();
0133 }
0134 }
0135 #endif
0136
0137 size_t size() const
0138 {
0139 boost::shared_lock<shared_mutex> guard(m);
0140 return threads.size();
0141 }
0142
0143 private:
0144 std::list<thread*> threads;
0145 mutable shared_mutex m;
0146 };
0147 }
0148
0149 #ifdef BOOST_MSVC
0150 #pragma warning(pop)
0151 #endif
0152
0153 #include <boost/config/abi_suffix.hpp>
0154
0155 #endif