test_5351.cpp 1.1 KB

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