1 // Copyright (C) 2001-2003
3 // Copyright (C) 2008 Anthony Williams
5 // Distributed under the Boost Software License, Version 1.0. (See accompanying
6 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 #define BOOST_THREAD_VERSION 2
9 #define BOOST_THREAD_PROVIDES_INTERRUPTIONS
11 #include <boost/thread/detail/config.hpp>
13 #include <boost/thread/thread_only.hpp>
14 #include <boost/thread/xtime.hpp>
15 #include <boost/bind/bind.hpp>
16 #include <boost/ref.hpp>
17 #include <boost/utility.hpp>
19 #define BOOST_TEST_MODULE Boost.Threads: thread test suite
21 #include <boost/test/unit_test.hpp>
23 #define DEFAULT_EXECUTION_MONITOR_TYPE execution_monitor::use_sleep_only
33 void comparison_thread(boost::thread::id parent
)
35 boost::thread::id
const my_id
=boost::this_thread::get_id();
37 BOOST_CHECK(my_id
!= parent
);
38 boost::thread::id
const my_id2
=boost::this_thread::get_id();
39 BOOST_CHECK(my_id
== my_id2
);
41 boost::thread::id
const no_thread_id
=boost::thread::id();
42 BOOST_CHECK(my_id
!= no_thread_id
);
45 BOOST_AUTO_TEST_CASE(test_sleep
)
47 boost::xtime xt
= delay(3);
48 boost::thread::sleep(xt
);
50 // Ensure it's in a range instead of checking actual equality due to time
52 BOOST_CHECK(boost::threads::test::in_range(xt
, 2));
55 void do_test_creation()
58 boost::thread
thrd(&simple_thread
);
60 BOOST_CHECK_EQUAL(test_value
, 999);
63 BOOST_AUTO_TEST_CASE(test_creation
)
65 timed_test(&do_test_creation
, 1);
68 void do_test_id_comparison()
70 boost::thread::id
const self
=boost::this_thread::get_id();
71 boost::thread
thrd(boost::bind(&comparison_thread
, self
));
75 BOOST_AUTO_TEST_CASE(test_id_comparison
)
77 timed_test(&do_test_id_comparison
, 1);
80 void interruption_point_thread(boost::mutex
* m
,bool* failed
)
82 boost::unique_lock
<boost::mutex
> lk(*m
);
83 boost::this_thread::interruption_point();
87 void do_test_thread_interrupts_at_interruption_point()
91 boost::unique_lock
<boost::mutex
> lk(m
);
92 boost::thread
thrd(boost::bind(&interruption_point_thread
,&m
,&failed
));
99 BOOST_AUTO_TEST_CASE(test_thread_interrupts_at_interruption_point
)
101 timed_test(&do_test_thread_interrupts_at_interruption_point
, 1);
104 void disabled_interruption_point_thread(boost::mutex
* m
,bool* failed
)
106 boost::unique_lock
<boost::mutex
> lk(*m
);
107 boost::this_thread::disable_interruption dc
;
108 boost::this_thread::interruption_point();
112 void do_test_thread_no_interrupt_if_interrupts_disabled_at_interruption_point()
116 boost::unique_lock
<boost::mutex
> lk(m
);
117 boost::thread
thrd(boost::bind(&disabled_interruption_point_thread
,&m
,&failed
));
121 BOOST_CHECK(!failed
);
124 BOOST_AUTO_TEST_CASE(test_thread_no_interrupt_if_interrupts_disabled_at_interruption_point
)
126 timed_test(&do_test_thread_no_interrupt_if_interrupts_disabled_at_interruption_point
, 1);
129 struct non_copyable_functor
:
134 non_copyable_functor(): boost::noncopyable(),
144 void do_test_creation_through_reference_wrapper()
146 non_copyable_functor f
;
148 boost::thread
thrd(boost::ref(f
));
150 BOOST_CHECK_EQUAL(f
.value
, 999u);
153 BOOST_AUTO_TEST_CASE(test_creation_through_reference_wrapper
)
155 timed_test(&do_test_creation_through_reference_wrapper
, 1);
158 struct long_running_thread
160 boost::condition_variable cond
;
164 long_running_thread():
170 boost::unique_lock
<boost::mutex
> lk(mut
);
178 void do_test_timed_join()
180 long_running_thread f
;
181 boost::thread
thrd(boost::ref(f
));
182 BOOST_CHECK(thrd
.joinable());
183 boost::system_time xt
=delay(3);
184 bool const joined
=thrd
.timed_join(xt
);
185 BOOST_CHECK(boost::threads::test::in_range(boost::get_xtime(xt
), 2));
186 BOOST_CHECK(!joined
);
187 BOOST_CHECK(thrd
.joinable());
189 boost::unique_lock
<boost::mutex
> lk(f
.mut
);
195 bool const joined2
=thrd
.timed_join(xt
);
196 boost::system_time
const now
=boost::get_system_time();
198 BOOST_CHECK(joined2
);
199 BOOST_CHECK(!thrd
.joinable());
202 BOOST_AUTO_TEST_CASE(test_timed_join
)
204 timed_test(&do_test_timed_join
, 10);
207 BOOST_AUTO_TEST_CASE(test_swap
)
209 boost::thread
t(&simple_thread
);
210 boost::thread
t2(&simple_thread
);
211 boost::thread::id id1
=t
.get_id();
212 boost::thread::id id2
=t2
.get_id();
215 BOOST_CHECK(t
.get_id()==id2
);
216 BOOST_CHECK(t2
.get_id()==id1
);
219 BOOST_CHECK(t
.get_id()==id1
);
220 BOOST_CHECK(t2
.get_id()==id2
);