wait_until_pass.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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/condition_variable>
  14. // class condition_variable;
  15. // condition_variable(const condition_variable&) = delete;
  16. #include <boost/thread/condition_variable.hpp>
  17. #include <boost/thread/mutex.hpp>
  18. #include <boost/thread/thread.hpp>
  19. #include <boost/detail/lightweight_test.hpp>
  20. #include <iostream>
  21. #include <cassert>
  22. #include "../../../timming.hpp"
  23. #if defined BOOST_THREAD_USES_CHRONO
  24. typedef boost::chrono::milliseconds ms;
  25. typedef boost::chrono::nanoseconds ns;
  26. struct Clock
  27. {
  28. typedef boost::chrono::milliseconds duration;
  29. typedef duration::rep rep;
  30. typedef duration::period period;
  31. typedef boost::chrono::time_point<Clock> time_point;
  32. static const bool is_steady = true;
  33. static time_point now()
  34. {
  35. using namespace boost::chrono;
  36. return time_point(duration_cast<duration> (steady_clock::now().time_since_epoch()));
  37. }
  38. };
  39. boost::condition_variable cv;
  40. boost::mutex mut;
  41. int test1 = 0;
  42. int test2 = 0;
  43. int runs = 0;
  44. const ms max_diff(BOOST_THREAD_TEST_TIME_MS);
  45. void f()
  46. {
  47. try {
  48. boost::unique_lock < boost::mutex > lk(mut);
  49. assert(test2 == 0);
  50. test1 = 1;
  51. cv.notify_one();
  52. Clock::time_point t0 = Clock::now();
  53. Clock::time_point t = t0 + Clock::duration(250);
  54. while (test2 == 0 && cv.wait_until(lk, t) == boost::cv_status::no_timeout) {}
  55. Clock::time_point t1 = Clock::now();
  56. if (runs == 0)
  57. {
  58. ns d = t1 - t0;
  59. BOOST_THREAD_TEST_IT(d, ns(max_diff));
  60. assert(test2 != 0);
  61. }
  62. else
  63. {
  64. ns d = t1 - t0 - Clock::duration(250);
  65. BOOST_THREAD_TEST_IT(d, ns(max_diff));
  66. assert(test2 == 0);
  67. }
  68. ++runs;
  69. } catch(...) {
  70. assert(false);
  71. std::cout << "ERROR exception" << __LINE__ << std::endl;
  72. }
  73. }
  74. int main()
  75. {
  76. try
  77. {
  78. boost::unique_lock < boost::mutex > lk(mut);
  79. boost::thread t(f);
  80. BOOST_TEST(test1 == 0);
  81. while (test1 == 0)
  82. cv.wait(lk);
  83. BOOST_TEST(test1 != 0);
  84. test2 = 1;
  85. lk.unlock();
  86. cv.notify_one();
  87. t.join();
  88. } catch(...) {
  89. BOOST_TEST(false);
  90. std::cout << "ERROR exception" << __LINE__ << std::endl;
  91. }
  92. test1 = 0;
  93. test2 = 0;
  94. try
  95. {
  96. boost::unique_lock < boost::mutex > lk(mut);
  97. boost::thread t(f);
  98. BOOST_TEST(test1 == 0);
  99. while (test1 == 0)
  100. cv.wait(lk);
  101. BOOST_TEST(test1 != 0);
  102. lk.unlock();
  103. t.join();
  104. } catch(...) {
  105. BOOST_TEST(false);
  106. std::cout << "ERROR exception" << __LINE__ << std::endl;
  107. }
  108. return boost::report_errors();
  109. }
  110. #else
  111. #error "Test not applicable: BOOST_THREAD_USES_CHRONO not defined for this platform as not supported"
  112. #endif