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/date_time/posix_time/posix_time_types.hpp> |
11 | #include <boost/thread/future.hpp> |
12 | |
13 | using namespace boost::posix_time; |
14 | using namespace boost; |
15 | |
16 | int foo() |
17 | { |
18 | this_thread::sleep(rel_time: seconds(10)); |
19 | return 0; |
20 | } |
21 | |
22 | |
23 | int main() |
24 | { |
25 | boost::packaged_task<int> pt(&foo); |
26 | boost::unique_future<int> fi = pt.get_future(); |
27 | boost::thread task(boost::move(t&: pt)); // launch task on a thread |
28 | |
29 | task.interrupt(); |
30 | |
31 | try |
32 | { |
33 | int v = fi.get(); |
34 | } |
35 | catch (boost::thread_interrupted& exc) |
36 | { |
37 | std::cout << "OK: "<< std::endl; |
38 | return 0; |
39 | } |
40 | catch (boost::exception& exc) |
41 | { |
42 | std::cout << __LINE__ << " ERROR: "<< boost::diagnostic_information(e: exc) << std::endl; |
43 | return 1; |
44 | } |
45 | catch (...) |
46 | { |
47 | std::cout << __LINE__ << " ERROR: "<< std::endl; |
48 | return 2; |
49 | } |
50 | std::cout << __LINE__ << " ERROR: "<< std::endl; |
51 | return 3; |
52 | } |
53 |