Ticket #8323: test.cpp

File test.cpp, 1000 bytes (added by szakharchenko@…, 10 years ago)

Simple test case (should hang with 99% certainty)

Line 
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
8using boost::thread;
9using boost::this_thread::sleep_for;
10using boost::chrono::milliseconds;
11using boost::chrono::system_clock;
12using std::cout;
13using std::endl;
14
15void rather_lazy_thread()
16{
17 while (true) sleep_for(milliseconds(10000));
18}
19
20BOOST_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}