thread_group.hpp 4.1 KB

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