test_condition_notify_all.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // Copyright (C) 2007 Anthony Williams
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #define BOOST_THREAD_VERSION 2
  6. #define BOOST_TEST_MODULE Boost.Threads: condition_variable notify_all test suite
  7. #include <boost/thread/detail/config.hpp>
  8. #include <boost/thread/thread_only.hpp>
  9. #include <boost/test/unit_test.hpp>
  10. #include "./util.inl"
  11. #include "./condition_test_common.hpp"
  12. unsigned const number_of_test_threads=5;
  13. void do_test_condition_notify_all_wakes_from_wait()
  14. {
  15. wait_for_flag data;
  16. boost::thread_group group;
  17. try
  18. {
  19. for(unsigned i=0;i<number_of_test_threads;++i)
  20. {
  21. group.create_thread(bind(&wait_for_flag::wait_without_predicate, data));
  22. }
  23. {
  24. boost::unique_lock<boost::mutex> lock(data.mutex);
  25. data.flag=true;
  26. data.cond_var.notify_all();
  27. }
  28. group.join_all();
  29. BOOST_CHECK_EQUAL(data.woken,number_of_test_threads);
  30. }
  31. catch(...)
  32. {
  33. group.join_all();
  34. throw;
  35. }
  36. }
  37. void do_test_condition_notify_all_wakes_from_wait_with_predicate()
  38. {
  39. wait_for_flag data;
  40. boost::thread_group group;
  41. try
  42. {
  43. for(unsigned i=0;i<number_of_test_threads;++i)
  44. {
  45. group.create_thread(bind(&wait_for_flag::wait_with_predicate, data));
  46. }
  47. {
  48. boost::unique_lock<boost::mutex> lock(data.mutex);
  49. data.flag=true;
  50. data.cond_var.notify_all();
  51. }
  52. group.join_all();
  53. BOOST_CHECK_EQUAL(data.woken,number_of_test_threads);
  54. }
  55. catch(...)
  56. {
  57. group.join_all();
  58. throw;
  59. }
  60. }
  61. void do_test_condition_notify_all_wakes_from_timed_wait()
  62. {
  63. wait_for_flag data;
  64. boost::thread_group group;
  65. try
  66. {
  67. for(unsigned i=0;i<number_of_test_threads;++i)
  68. {
  69. group.create_thread(bind(&wait_for_flag::timed_wait_without_predicate, data));
  70. }
  71. {
  72. boost::unique_lock<boost::mutex> lock(data.mutex);
  73. data.flag=true;
  74. data.cond_var.notify_all();
  75. }
  76. group.join_all();
  77. BOOST_CHECK_EQUAL(data.woken,number_of_test_threads);
  78. }
  79. catch(...)
  80. {
  81. group.join_all();
  82. throw;
  83. }
  84. }
  85. void do_test_condition_notify_all_wakes_from_timed_wait_with_predicate()
  86. {
  87. wait_for_flag data;
  88. boost::thread_group group;
  89. try
  90. {
  91. for(unsigned i=0;i<number_of_test_threads;++i)
  92. {
  93. group.create_thread(bind(&wait_for_flag::timed_wait_with_predicate, data));
  94. }
  95. {
  96. boost::unique_lock<boost::mutex> lock(data.mutex);
  97. data.flag=true;
  98. data.cond_var.notify_all();
  99. }
  100. group.join_all();
  101. BOOST_CHECK_EQUAL(data.woken,number_of_test_threads);
  102. }
  103. catch(...)
  104. {
  105. group.join_all();
  106. throw;
  107. }
  108. }
  109. void do_test_condition_notify_all_wakes_from_relative_timed_wait_with_predicate()
  110. {
  111. wait_for_flag data;
  112. boost::thread_group group;
  113. try
  114. {
  115. for(unsigned i=0;i<number_of_test_threads;++i)
  116. {
  117. group.create_thread(bind(&wait_for_flag::relative_timed_wait_with_predicate, data));
  118. }
  119. {
  120. boost::unique_lock<boost::mutex> lock(data.mutex);
  121. data.flag=true;
  122. data.cond_var.notify_all();
  123. }
  124. group.join_all();
  125. BOOST_CHECK_EQUAL(data.woken,number_of_test_threads);
  126. }
  127. catch(...)
  128. {
  129. group.join_all();
  130. throw;
  131. }
  132. }
  133. namespace
  134. {
  135. boost::mutex multiple_wake_mutex;
  136. boost::condition_variable multiple_wake_cond;
  137. unsigned multiple_wake_count=0;
  138. void wait_for_condvar_and_increase_count()
  139. {
  140. boost::unique_lock<boost::mutex> lk(multiple_wake_mutex);
  141. multiple_wake_cond.wait(lk);
  142. ++multiple_wake_count;
  143. }
  144. }
  145. void do_test_notify_all_following_notify_one_wakes_all_threads()
  146. {
  147. boost::thread thread1(&wait_for_condvar_and_increase_count);
  148. boost::thread thread2(&wait_for_condvar_and_increase_count);
  149. boost::this_thread::sleep(boost::posix_time::milliseconds(200));
  150. multiple_wake_cond.notify_one();
  151. boost::thread thread3(&wait_for_condvar_and_increase_count);
  152. boost::this_thread::sleep(boost::posix_time::milliseconds(200));
  153. multiple_wake_cond.notify_one();
  154. multiple_wake_cond.notify_all();
  155. boost::this_thread::sleep(boost::posix_time::milliseconds(200));
  156. {
  157. boost::unique_lock<boost::mutex> lk(multiple_wake_mutex);
  158. BOOST_CHECK(multiple_wake_count==3);
  159. }
  160. thread1.join();
  161. thread2.join();
  162. thread3.join();
  163. }
  164. BOOST_AUTO_TEST_CASE(test_condition_notify_all)
  165. {
  166. timed_test(&do_test_condition_notify_all_wakes_from_wait, timeout_seconds);
  167. timed_test(&do_test_condition_notify_all_wakes_from_wait_with_predicate, timeout_seconds);
  168. timed_test(&do_test_condition_notify_all_wakes_from_timed_wait, timeout_seconds);
  169. timed_test(&do_test_condition_notify_all_wakes_from_timed_wait_with_predicate, timeout_seconds);
  170. timed_test(&do_test_condition_notify_all_wakes_from_relative_timed_wait_with_predicate, timeout_seconds);
  171. timed_test(&do_test_notify_all_following_notify_one_wakes_all_threads, timeout_seconds);
  172. }