1 // Copyright (C) 2007 Anthony Williams
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)
6 #define BOOST_THREAD_VERSION 2
8 #define BOOST_TEST_MODULE Boost.Threads: condition_variable notify_one test suite
10 #include <boost/thread/detail/config.hpp>
12 #include <boost/thread/thread_only.hpp>
14 #include <boost/test/unit_test.hpp>
17 #include "./condition_test_common.hpp"
19 void do_test_condition_notify_one_wakes_from_wait()
23 boost::thread
thread(bind(&wait_for_flag::wait_without_predicate
, data
));
26 boost::unique_lock
<boost::mutex
> lock(data
.mutex
);
28 data
.cond_var
.notify_one();
32 BOOST_CHECK(data
.woken
);
35 void do_test_condition_notify_one_wakes_from_wait_with_predicate()
39 boost::thread
thread(bind(&wait_for_flag::wait_with_predicate
, data
));
42 boost::unique_lock
<boost::mutex
> lock(data
.mutex
);
44 data
.cond_var
.notify_one();
48 BOOST_CHECK(data
.woken
);
51 void do_test_condition_notify_one_wakes_from_timed_wait()
55 boost::thread
thread(bind(&wait_for_flag::timed_wait_without_predicate
, data
));
58 boost::unique_lock
<boost::mutex
> lock(data
.mutex
);
60 data
.cond_var
.notify_one();
64 BOOST_CHECK(data
.woken
);
67 void do_test_condition_notify_one_wakes_from_timed_wait_with_predicate()
71 boost::thread
thread(bind(&wait_for_flag::timed_wait_with_predicate
, data
));
74 boost::unique_lock
<boost::mutex
> lock(data
.mutex
);
76 data
.cond_var
.notify_one();
80 BOOST_CHECK(data
.woken
);
83 void do_test_condition_notify_one_wakes_from_relative_timed_wait_with_predicate()
87 boost::thread
thread(bind(&wait_for_flag::relative_timed_wait_with_predicate
, data
));
90 boost::unique_lock
<boost::mutex
> lock(data
.mutex
);
92 data
.cond_var
.notify_one();
96 BOOST_CHECK(data
.woken
);
101 boost::mutex multiple_wake_mutex
;
102 boost::condition_variable multiple_wake_cond
;
103 unsigned multiple_wake_count
=0;
105 void wait_for_condvar_and_increase_count()
107 boost::unique_lock
<boost::mutex
> lk(multiple_wake_mutex
);
108 multiple_wake_cond
.wait(lk
);
109 ++multiple_wake_count
;
115 void do_test_multiple_notify_one_calls_wakes_multiple_threads()
117 boost::thread
thread1(&wait_for_condvar_and_increase_count
);
118 boost::thread
thread2(&wait_for_condvar_and_increase_count
);
120 boost::this_thread::sleep(boost::posix_time::milliseconds(200));
121 multiple_wake_cond
.notify_one();
123 boost::thread
thread3(&wait_for_condvar_and_increase_count
);
125 boost::this_thread::sleep(boost::posix_time::milliseconds(200));
126 multiple_wake_cond
.notify_one();
127 multiple_wake_cond
.notify_one();
128 boost::this_thread::sleep(boost::posix_time::milliseconds(200));
131 boost::unique_lock
<boost::mutex
> lk(multiple_wake_mutex
);
132 BOOST_CHECK(multiple_wake_count
==3);
140 BOOST_AUTO_TEST_CASE(test_condition_notify_one
)
142 timed_test(&do_test_condition_notify_one_wakes_from_wait
, timeout_seconds
, execution_monitor::use_mutex
);
143 timed_test(&do_test_condition_notify_one_wakes_from_wait_with_predicate
, timeout_seconds
, execution_monitor::use_mutex
);
144 timed_test(&do_test_condition_notify_one_wakes_from_timed_wait
, timeout_seconds
, execution_monitor::use_mutex
);
145 timed_test(&do_test_condition_notify_one_wakes_from_timed_wait_with_predicate
, timeout_seconds
, execution_monitor::use_mutex
);
146 timed_test(&do_test_condition_notify_one_wakes_from_relative_timed_wait_with_predicate
, timeout_seconds
, execution_monitor::use_mutex
);
147 timed_test(&do_test_multiple_notify_one_calls_wakes_multiple_threads
, timeout_seconds
, execution_monitor::use_mutex
);