test_condition_notify_one.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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_one 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. void do_test_condition_notify_one_wakes_from_wait()
  13. {
  14. wait_for_flag data;
  15. boost::thread thread(bind(&wait_for_flag::wait_without_predicate, data));
  16. {
  17. boost::unique_lock<boost::mutex> lock(data.mutex);
  18. data.flag=true;
  19. data.cond_var.notify_one();
  20. }
  21. thread.join();
  22. BOOST_CHECK(data.woken);
  23. }
  24. void do_test_condition_notify_one_wakes_from_wait_with_predicate()
  25. {
  26. wait_for_flag data;
  27. boost::thread thread(bind(&wait_for_flag::wait_with_predicate, data));
  28. {
  29. boost::unique_lock<boost::mutex> lock(data.mutex);
  30. data.flag=true;
  31. data.cond_var.notify_one();
  32. }
  33. thread.join();
  34. BOOST_CHECK(data.woken);
  35. }
  36. void do_test_condition_notify_one_wakes_from_timed_wait()
  37. {
  38. wait_for_flag data;
  39. boost::thread thread(bind(&wait_for_flag::timed_wait_without_predicate, data));
  40. {
  41. boost::unique_lock<boost::mutex> lock(data.mutex);
  42. data.flag=true;
  43. data.cond_var.notify_one();
  44. }
  45. thread.join();
  46. BOOST_CHECK(data.woken);
  47. }
  48. void do_test_condition_notify_one_wakes_from_timed_wait_with_predicate()
  49. {
  50. wait_for_flag data;
  51. boost::thread thread(bind(&wait_for_flag::timed_wait_with_predicate, data));
  52. {
  53. boost::unique_lock<boost::mutex> lock(data.mutex);
  54. data.flag=true;
  55. data.cond_var.notify_one();
  56. }
  57. thread.join();
  58. BOOST_CHECK(data.woken);
  59. }
  60. void do_test_condition_notify_one_wakes_from_relative_timed_wait_with_predicate()
  61. {
  62. wait_for_flag data;
  63. boost::thread thread(bind(&wait_for_flag::relative_timed_wait_with_predicate, data));
  64. {
  65. boost::unique_lock<boost::mutex> lock(data.mutex);
  66. data.flag=true;
  67. data.cond_var.notify_one();
  68. }
  69. thread.join();
  70. BOOST_CHECK(data.woken);
  71. }
  72. namespace
  73. {
  74. boost::mutex multiple_wake_mutex;
  75. boost::condition_variable multiple_wake_cond;
  76. unsigned multiple_wake_count=0;
  77. void wait_for_condvar_and_increase_count()
  78. {
  79. boost::unique_lock<boost::mutex> lk(multiple_wake_mutex);
  80. multiple_wake_cond.wait(lk);
  81. ++multiple_wake_count;
  82. }
  83. }
  84. void do_test_multiple_notify_one_calls_wakes_multiple_threads()
  85. {
  86. boost::thread thread1(&wait_for_condvar_and_increase_count);
  87. boost::thread thread2(&wait_for_condvar_and_increase_count);
  88. boost::this_thread::sleep(boost::posix_time::milliseconds(200));
  89. multiple_wake_cond.notify_one();
  90. boost::thread thread3(&wait_for_condvar_and_increase_count);
  91. boost::this_thread::sleep(boost::posix_time::milliseconds(200));
  92. multiple_wake_cond.notify_one();
  93. multiple_wake_cond.notify_one();
  94. boost::this_thread::sleep(boost::posix_time::milliseconds(200));
  95. {
  96. boost::unique_lock<boost::mutex> lk(multiple_wake_mutex);
  97. BOOST_CHECK(multiple_wake_count==3);
  98. }
  99. thread1.join();
  100. thread2.join();
  101. thread3.join();
  102. }
  103. BOOST_AUTO_TEST_CASE(test_condition_notify_one)
  104. {
  105. timed_test(&do_test_condition_notify_one_wakes_from_wait, timeout_seconds, execution_monitor::use_mutex);
  106. timed_test(&do_test_condition_notify_one_wakes_from_wait_with_predicate, timeout_seconds, execution_monitor::use_mutex);
  107. timed_test(&do_test_condition_notify_one_wakes_from_timed_wait, timeout_seconds, execution_monitor::use_mutex);
  108. timed_test(&do_test_condition_notify_one_wakes_from_timed_wait_with_predicate, timeout_seconds, execution_monitor::use_mutex);
  109. timed_test(&do_test_condition_notify_one_wakes_from_relative_timed_wait_with_predicate, timeout_seconds, execution_monitor::use_mutex);
  110. timed_test(&do_test_multiple_notify_one_calls_wakes_multiple_threads, timeout_seconds, execution_monitor::use_mutex);
  111. }