1 | #include <boost/chrono/system_clocks.hpp>
|
---|
2 | #include <boost/foreach.hpp>
|
---|
3 | #include <boost/thread.hpp>
|
---|
4 | #pragma hdrstop
|
---|
5 | #define BOOST_TEST_MAIN
|
---|
6 | #include <boost/test/unit_test.hpp>
|
---|
7 |
|
---|
8 | using boost::thread;
|
---|
9 | using boost::this_thread::sleep_for;
|
---|
10 | using boost::chrono::milliseconds;
|
---|
11 | using boost::chrono::system_clock;
|
---|
12 | using std::cout;
|
---|
13 | using std::endl;
|
---|
14 |
|
---|
15 | void rather_lazy_thread()
|
---|
16 | {
|
---|
17 | while (true) sleep_for(milliseconds(10000));
|
---|
18 | }
|
---|
19 |
|
---|
20 | BOOST_AUTO_TEST_CASE(try_join_test)
|
---|
21 | {
|
---|
22 | thread t(rather_lazy_thread);
|
---|
23 | cout<<"This doesn't hang (waits 1 msec)"<<endl;
|
---|
24 | t.try_join_until(system_clock::now()+milliseconds(1));
|
---|
25 | cout<<"This doesn't hang either, though time is in the past"<<endl;
|
---|
26 | t.try_join_until(system_clock::now()-milliseconds(2));
|
---|
27 | cout<<"But this does, unless now() calls are separated by more than a millisecond, which is mostly never"<<endl;
|
---|
28 | t.try_join_until(system_clock::now()-milliseconds(1));
|
---|
29 | cout<<"So none of the following will happen"<<endl;
|
---|
30 | t.interrupt();
|
---|
31 | t.join();
|
---|
32 | }
|
---|