| 1 | #include <iostream>
|
|---|
| 2 | #include <boost/thread.hpp>
|
|---|
| 3 | #include <boost/date_time/posix_time/posix_time_types.hpp>
|
|---|
| 4 | #include <boost/thread/future.hpp>
|
|---|
| 5 |
|
|---|
| 6 | using namespace boost::posix_time;
|
|---|
| 7 | using namespace boost;
|
|---|
| 8 |
|
|---|
| 9 | int foo()
|
|---|
| 10 | {
|
|---|
| 11 | this_thread::sleep(seconds(10));
|
|---|
| 12 | return 0;
|
|---|
| 13 | }
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 | int main(int argc, char** argv)
|
|---|
| 17 | {
|
|---|
| 18 | boost::packaged_task<int> pt(&foo);
|
|---|
| 19 | boost::unique_future<int> fi = pt.get_future();
|
|---|
| 20 | boost::thread task(boost::move(pt)); // launch task on a thread
|
|---|
| 21 |
|
|---|
| 22 | task.interrupt();
|
|---|
| 23 |
|
|---|
| 24 | try
|
|---|
| 25 | {
|
|---|
| 26 | int v = fi.get();
|
|---|
| 27 | }
|
|---|
| 28 | catch (boost::thread_interrupted& exc)
|
|---|
| 29 | {
|
|---|
| 30 | std::cout << "OK: " << std::endl;
|
|---|
| 31 | return 0;
|
|---|
| 32 | }
|
|---|
| 33 | catch (boost::exception& exc)
|
|---|
| 34 | {
|
|---|
| 35 | std::cout << __LINE__ << " ERROR: " << boost::diagnostic_information(exc) << std::endl;
|
|---|
| 36 | return 1;
|
|---|
| 37 | }
|
|---|
| 38 | catch (...)
|
|---|
| 39 | {
|
|---|
| 40 | std::cout << __LINE__ << " ERROR: " << std::endl;
|
|---|
| 41 | return 2;
|
|---|
| 42 | }
|
|---|
| 43 | std::cout << __LINE__ << " ERROR: " << std::endl;
|
|---|
| 44 | return 3;
|
|---|
| 45 | }
|
|---|