sleep_until_pass.cpp 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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::steady_clock Clock;
  24. typedef Clock::time_point time_point;
  25. //typedef Clock::duration duration;
  26. boost::chrono::milliseconds ms(500);
  27. time_point t0 = Clock::now();
  28. boost::this_thread::sleep_until(t0 + ms);
  29. time_point t1 = Clock::now();
  30. boost::chrono::nanoseconds ns = (t1 - t0) - ms;
  31. boost::chrono::nanoseconds err = ms / 100;
  32. // The time slept is within 1% of 500ms
  33. // This test is spurious as it depends on the time the thread system switches the threads
  34. BOOST_TEST((std::max)(ns.count(), -ns.count()) < (err+boost::chrono::milliseconds(1000)).count());
  35. //BOOST_TEST(std::abs(static_cast<long>(ns.count())) < (err+boost::chrono::milliseconds(1000)).count());
  36. }
  37. {
  38. typedef boost::chrono::system_clock Clock;
  39. typedef Clock::time_point time_point;
  40. //typedef Clock::duration duration;
  41. boost::chrono::milliseconds ms(500);
  42. time_point t0 = Clock::now();
  43. boost::this_thread::sleep_until(t0 + ms);
  44. time_point t1 = Clock::now();
  45. boost::chrono::nanoseconds ns = (t1 - t0) - ms;
  46. boost::chrono::nanoseconds err = ms / 100;
  47. // The time slept is within 1% of 500ms
  48. // This test is spurious as it depends on the time the thread system switches the threads
  49. BOOST_TEST((std::max)(ns.count(), -ns.count()) < (err+boost::chrono::milliseconds(1000)).count());
  50. //BOOST_TEST(std::abs(static_cast<long>(ns.count())) < (err+boost::chrono::milliseconds(1000)).count());
  51. }
  52. {
  53. typedef boost::chrono::steady_clock Clock;
  54. typedef Clock::time_point time_point;
  55. //typedef Clock::duration duration;
  56. boost::chrono::milliseconds ms(500);
  57. time_point t0 = Clock::now();
  58. boost::this_thread::no_interruption_point::sleep_until(t0 + ms);
  59. time_point t1 = Clock::now();
  60. boost::chrono::nanoseconds ns = (t1 - t0) - ms;
  61. boost::chrono::nanoseconds err = ms / 100;
  62. // The time slept is within 1% of 500ms
  63. // This test is spurious as it depends on the time the thread system switches the threads
  64. BOOST_TEST((std::max)(ns.count(), -ns.count()) < (err+boost::chrono::milliseconds(1000)).count());
  65. //BOOST_TEST(std::abs(static_cast<long>(ns.count())) < (err+boost::chrono::milliseconds(1000)).count());
  66. }
  67. {
  68. typedef boost::chrono::system_clock Clock;
  69. typedef Clock::time_point time_point;
  70. //typedef Clock::duration duration;
  71. boost::chrono::milliseconds ms(500);
  72. time_point t0 = Clock::now();
  73. boost::this_thread::no_interruption_point::sleep_until(t0 + ms);
  74. time_point t1 = Clock::now();
  75. boost::chrono::nanoseconds ns = (t1 - t0) - ms;
  76. boost::chrono::nanoseconds err = ms / 100;
  77. // The time slept is within 1% of 500ms
  78. // This test is spurious as it depends on the time the thread system switches the threads
  79. BOOST_TEST((std::max)(ns.count(), -ns.count()) < (err+boost::chrono::milliseconds(1000)).count());
  80. //BOOST_TEST(std::abs(static_cast<long>(ns.count())) < (err+boost::chrono::milliseconds(1000)).count());
  81. }
  82. return boost::report_errors();
  83. }
  84. #else
  85. #error "Test not applicable: BOOST_THREAD_USES_CHRONO not defined for this platform as not supported"
  86. #endif