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
7 #define BOOST_TEST_MODULE Boost.Threads: condition_variable notify_all test suite
8 #include <boost/thread/detail/config.hpp>
10 #include <boost/thread/thread_only.hpp>
12 #include <boost/test/unit_test.hpp>
15 #include "./condition_test_common.hpp"
17 unsigned const number_of_test_threads
=5;
19 void do_test_condition_notify_all_wakes_from_wait()
23 boost::thread_group group
;
27 for(unsigned i
=0;i
<number_of_test_threads
;++i
)
29 group
.create_thread(bind(&wait_for_flag::wait_without_predicate
, data
));
33 boost::unique_lock
<boost::mutex
> lock(data
.mutex
);
35 data
.cond_var
.notify_all();
39 BOOST_CHECK_EQUAL(data
.woken
,number_of_test_threads
);
48 void do_test_condition_notify_all_wakes_from_wait_with_predicate()
52 boost::thread_group group
;
56 for(unsigned i
=0;i
<number_of_test_threads
;++i
)
58 group
.create_thread(bind(&wait_for_flag::wait_with_predicate
, data
));
62 boost::unique_lock
<boost::mutex
> lock(data
.mutex
);
64 data
.cond_var
.notify_all();
68 BOOST_CHECK_EQUAL(data
.woken
,number_of_test_threads
);
77 void do_test_condition_notify_all_wakes_from_timed_wait()
81 boost::thread_group group
;
85 for(unsigned i
=0;i
<number_of_test_threads
;++i
)
87 group
.create_thread(bind(&wait_for_flag::timed_wait_without_predicate
, data
));
91 boost::unique_lock
<boost::mutex
> lock(data
.mutex
);
93 data
.cond_var
.notify_all();
97 BOOST_CHECK_EQUAL(data
.woken
,number_of_test_threads
);
106 void do_test_condition_notify_all_wakes_from_timed_wait_with_predicate()
110 boost::thread_group group
;
114 for(unsigned i
=0;i
<number_of_test_threads
;++i
)
116 group
.create_thread(bind(&wait_for_flag::timed_wait_with_predicate
, data
));
120 boost::unique_lock
<boost::mutex
> lock(data
.mutex
);
122 data
.cond_var
.notify_all();
126 BOOST_CHECK_EQUAL(data
.woken
,number_of_test_threads
);
135 void do_test_condition_notify_all_wakes_from_relative_timed_wait_with_predicate()
139 boost::thread_group group
;
143 for(unsigned i
=0;i
<number_of_test_threads
;++i
)
145 group
.create_thread(bind(&wait_for_flag::relative_timed_wait_with_predicate
, data
));
149 boost::unique_lock
<boost::mutex
> lock(data
.mutex
);
151 data
.cond_var
.notify_all();
155 BOOST_CHECK_EQUAL(data
.woken
,number_of_test_threads
);
166 boost::mutex multiple_wake_mutex
;
167 boost::condition_variable multiple_wake_cond
;
168 unsigned multiple_wake_count
=0;
170 void wait_for_condvar_and_increase_count()
172 boost::unique_lock
<boost::mutex
> lk(multiple_wake_mutex
);
173 multiple_wake_cond
.wait(lk
);
174 ++multiple_wake_count
;
180 void do_test_notify_all_following_notify_one_wakes_all_threads()
182 boost::thread
thread1(&wait_for_condvar_and_increase_count
);
183 boost::thread
thread2(&wait_for_condvar_and_increase_count
);
185 boost::this_thread::sleep(boost::posix_time::milliseconds(200));
186 multiple_wake_cond
.notify_one();
188 boost::thread
thread3(&wait_for_condvar_and_increase_count
);
190 boost::this_thread::sleep(boost::posix_time::milliseconds(200));
191 multiple_wake_cond
.notify_one();
192 multiple_wake_cond
.notify_all();
193 boost::this_thread::sleep(boost::posix_time::milliseconds(200));
196 boost::unique_lock
<boost::mutex
> lk(multiple_wake_mutex
);
197 BOOST_CHECK(multiple_wake_count
==3);
205 BOOST_AUTO_TEST_CASE(test_condition_notify_all
)
207 timed_test(&do_test_condition_notify_all_wakes_from_wait
, timeout_seconds
);
208 timed_test(&do_test_condition_notify_all_wakes_from_wait_with_predicate
, timeout_seconds
);
209 timed_test(&do_test_condition_notify_all_wakes_from_timed_wait
, timeout_seconds
);
210 timed_test(&do_test_condition_notify_all_wakes_from_timed_wait_with_predicate
, timeout_seconds
);
211 timed_test(&do_test_condition_notify_all_wakes_from_relative_timed_wait_with_predicate
, timeout_seconds
);
212 timed_test(&do_test_notify_all_following_notify_one_wakes_all_threads
, timeout_seconds
);