wait_until_pred_pass.cpp 2.8 KB

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