sleep_for_pass.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is dual licensed under the MIT and the University of Illinois Open
  6. // Source Licenses. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // Copyright (C) 2011 Vicente J. Botet Escriba
  10. //
  11. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  12. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  13. // <boost/thread/thread.hpp>
  14. // thread::id this_thread::get_id();
  15. #include <boost/thread/thread_only.hpp>
  16. #include <cstdlib>
  17. #include <algorithm>
  18. #include <boost/detail/lightweight_test.hpp>
  19. #if defined BOOST_THREAD_USES_CHRONO
  20. int main()
  21. {
  22. {
  23. typedef boost::chrono::system_clock Clock;
  24. typedef Clock::time_point time_point;
  25. boost::chrono::milliseconds ms(500);
  26. time_point t0 = Clock::now();
  27. boost::this_thread::sleep_for(ms);
  28. time_point t1 = Clock::now();
  29. boost::chrono::nanoseconds ns = (t1 - t0) - ms;
  30. boost::chrono::nanoseconds err = ms / 100;
  31. // The time slept is within 1% of 500ms
  32. // This test is spurious as it depends on the time the thread system switches the threads
  33. BOOST_TEST((std::max)(ns.count(), -ns.count()) < (err+boost::chrono::milliseconds(1000)).count());
  34. //BOOST_TEST(std::abs(static_cast<long>(ns.count())) < (err+boost::chrono::milliseconds(1000)).count());
  35. }
  36. {
  37. typedef boost::chrono::system_clock Clock;
  38. typedef Clock::time_point time_point;
  39. boost::chrono::milliseconds ms(500);
  40. time_point t0 = Clock::now();
  41. boost::this_thread::no_interruption_point::sleep_for(ms);
  42. time_point t1 = Clock::now();
  43. boost::chrono::nanoseconds ns = (t1 - t0) - ms;
  44. boost::chrono::nanoseconds err = ms / 100;
  45. // The time slept is within 1% of 500ms
  46. // This test is spurious as it depends on the time the thread system switches the threads
  47. BOOST_TEST((std::max)(ns.count(), -ns.count()) < (err+boost::chrono::milliseconds(1000)).count());
  48. //BOOST_TEST(std::abs(static_cast<long>(ns.count())) < (err+boost::chrono::milliseconds(1000)).count());
  49. }
  50. return boost::report_errors();
  51. }
  52. #else
  53. #error "Test not applicable: BOOST_THREAD_USES_CHRONO not defined for this platform as not supported"
  54. #endif