1 | // Copyright (C) 2010 Vicente Botet |
---|---|
2 | // |
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) |
5 | |
6 | #define BOOST_THREAD_PROVIDES_INTERRUPTIONS |
7 | |
8 | #include <iostream> |
9 | #include <boost/thread/thread_only.hpp> |
10 | #include <boost/detail/lightweight_test.hpp> |
11 | |
12 | #if defined BOOST_THREAD_USES_CHRONO |
13 | |
14 | //using namespace boost; |
15 | using namespace boost::chrono; |
16 | |
17 | bool interrupted = false; |
18 | void f() |
19 | { |
20 | try |
21 | { |
22 | std::cout << "Starting sleep in thread"<< std::endl; |
23 | for (;;) |
24 | { |
25 | boost::this_thread::sleep_for(d: seconds(60)); |
26 | } |
27 | } |
28 | catch (const boost::thread_interrupted&) |
29 | { |
30 | interrupted = true; |
31 | std::cout << "Thread interrupted."<< std::endl; |
32 | } |
33 | } |
34 | |
35 | int main() |
36 | { |
37 | boost::thread t(f); |
38 | t.interrupt(); |
39 | t.join(); |
40 | std::cout << "Joined with thread."<< std::endl; |
41 | BOOST_TEST(interrupted); |
42 | return boost::report_errors(); |
43 | } |
44 | |
45 | #else |
46 | #error "Test not applicable: BOOST_THREAD_USES_CHRONO not defined for this platform as not supported" |
47 | #endif |
48 |