test_7328.cpp 1.0 KB

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