wait_for_pred_pass.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. class Pred
  23. {
  24. int& i_;
  25. public:
  26. explicit Pred(int& i) :
  27. i_(i)
  28. {
  29. }
  30. bool operator()()
  31. {
  32. return i_ != 0;
  33. }
  34. };
  35. boost::condition_variable_any cv;
  36. typedef boost::timed_mutex L0;
  37. typedef boost::unique_lock<L0> L1;
  38. L0 m0;
  39. int test1 = 0;
  40. int test2 = 0;
  41. int runs = 0;
  42. typedef boost::chrono::system_clock Clock;
  43. typedef boost::chrono::milliseconds milliseconds;
  44. typedef boost::chrono::milliseconds ms;
  45. typedef boost::chrono::nanoseconds ns;
  46. const ms max_diff(BOOST_THREAD_TEST_TIME_MS);
  47. void f()
  48. {
  49. L1 lk(m0);
  50. BOOST_TEST(test2 == 0);
  51. test1 = 1;
  52. cv.notify_one();
  53. Clock::time_point t0 = Clock::now();
  54. cv.wait_for(lk, milliseconds(250), Pred(test2));
  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. BOOST_TEST(test2 != 0);
  61. }
  62. else
  63. {
  64. ns d = t1 - t0 - ms(250);
  65. BOOST_THREAD_TEST_IT(d, ns(max_diff));
  66. BOOST_TEST(test2 == 0);
  67. }
  68. ++runs;
  69. }
  70. int main()
  71. {
  72. {
  73. L1 lk(m0);
  74. boost::thread t(f);
  75. BOOST_TEST(test1 == 0);
  76. while (test1 == 0)
  77. cv.wait(lk);
  78. BOOST_TEST(test1 != 0);
  79. test2 = 1;
  80. lk.unlock();
  81. cv.notify_one();
  82. t.join();
  83. }
  84. test1 = 0;
  85. test2 = 0;
  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. lk.unlock();
  94. t.join();
  95. }
  96. return boost::report_errors();
  97. }
  98. #else
  99. #error "Test not applicable: BOOST_THREAD_USES_CHRONO not defined for this platform as not supported"
  100. #endif