wait_for_pass.cpp 2.9 KB

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