thread_group.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #ifndef BOOST_THREAD_DETAIL_THREAD_GROUP_HPP
  2. #define BOOST_THREAD_DETAIL_THREAD_GROUP_HPP
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. // (C) Copyright 2007-9 Anthony Williams
  7. #include <list>
  8. #include <boost/thread/csbl/memory/unique_ptr.hpp>
  9. #include <boost/thread/shared_mutex.hpp>
  10. #include <boost/thread/mutex.hpp>
  11. #include <boost/thread/lock_guard.hpp>
  12. #include <boost/config/abi_prefix.hpp>
  13. #ifdef BOOST_MSVC
  14. #pragma warning(push)
  15. #pragma warning(disable:4251)
  16. #endif
  17. namespace boost
  18. {
  19. class thread_group
  20. {
  21. private:
  22. thread_group(thread_group const&);
  23. thread_group& operator=(thread_group const&);
  24. public:
  25. thread_group() {}
  26. ~thread_group()
  27. {
  28. for(std::list<thread*>::iterator it=threads.begin(),end=threads.end();
  29. it!=end;
  30. ++it)
  31. {
  32. delete *it;
  33. }
  34. }
  35. bool is_this_thread_in()
  36. {
  37. thread::id id = this_thread::get_id();
  38. boost::shared_lock<shared_mutex> guard(m);
  39. for(std::list<thread*>::iterator it=threads.begin(),end=threads.end();
  40. it!=end;
  41. ++it)
  42. {
  43. if ((*it)->get_id() == id)
  44. return true;
  45. }
  46. return false;
  47. }
  48. bool is_thread_in(thread* thrd)
  49. {
  50. if(thrd)
  51. {
  52. thread::id id = thrd->get_id();
  53. boost::shared_lock<shared_mutex> guard(m);
  54. for(std::list<thread*>::iterator it=threads.begin(),end=threads.end();
  55. it!=end;
  56. ++it)
  57. {
  58. if ((*it)->get_id() == id)
  59. return true;
  60. }
  61. return false;
  62. }
  63. else
  64. {
  65. return false;
  66. }
  67. }
  68. template<typename F>
  69. thread* create_thread(F threadfunc)
  70. {
  71. boost::lock_guard<shared_mutex> guard(m);
  72. boost::csbl::unique_ptr<thread> new_thread(new thread(threadfunc));
  73. threads.push_back(new_thread.get());
  74. return new_thread.release();
  75. }
  76. void add_thread(thread* thrd)
  77. {
  78. if(thrd)
  79. {
  80. BOOST_THREAD_ASSERT_PRECONDITION( ! is_thread_in(thrd) ,
  81. thread_resource_error(static_cast<int>(system::errc::resource_deadlock_would_occur), "boost::thread_group: trying to add a duplicated thread")
  82. );
  83. boost::lock_guard<shared_mutex> guard(m);
  84. threads.push_back(thrd);
  85. }
  86. }
  87. void remove_thread(thread* thrd)
  88. {
  89. boost::lock_guard<shared_mutex> guard(m);
  90. std::list<thread*>::iterator const it=std::find(threads.begin(),threads.end(),thrd);
  91. if(it!=threads.end())
  92. {
  93. threads.erase(it);
  94. }
  95. }
  96. void join_all()
  97. {
  98. BOOST_THREAD_ASSERT_PRECONDITION( ! is_this_thread_in() ,
  99. thread_resource_error(static_cast<int>(system::errc::resource_deadlock_would_occur), "boost::thread_group: trying joining itself")
  100. );
  101. boost::shared_lock<shared_mutex> guard(m);
  102. for(std::list<thread*>::iterator it=threads.begin(),end=threads.end();
  103. it!=end;
  104. ++it)
  105. {
  106. if ((*it)->joinable())
  107. (*it)->join();
  108. }
  109. }
  110. #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
  111. void interrupt_all()
  112. {
  113. boost::shared_lock<shared_mutex> guard(m);
  114. for(std::list<thread*>::iterator it=threads.begin(),end=threads.end();
  115. it!=end;
  116. ++it)
  117. {
  118. (*it)->interrupt();
  119. }
  120. }
  121. #endif
  122. size_t size() const
  123. {
  124. boost::shared_lock<shared_mutex> guard(m);
  125. return threads.size();
  126. }
  127. private:
  128. std::list<thread*> threads;
  129. mutable shared_mutex m;
  130. };
  131. }
  132. #ifdef BOOST_MSVC
  133. #pragma warning(pop)
  134. #endif
  135. #include <boost/config/abi_suffix.hpp>
  136. #endif