wait_pass.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. // void wait(unique_lock<mutex>& lock);
  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 <cassert>
  21. #include <iostream>
  22. #if defined BOOST_THREAD_USES_CHRONO
  23. boost::condition_variable cv;
  24. boost::mutex mut;
  25. int test1 = 0;
  26. int test2 = 0;
  27. int runs = 0;
  28. void f()
  29. {
  30. try {
  31. boost::unique_lock<boost::mutex> lk(mut);
  32. assert(test2 == 0);
  33. test1 = 1;
  34. cv.notify_one();
  35. while (test2 == 0) {
  36. cv.wait(lk);
  37. }
  38. assert(test2 != 0);
  39. } catch(...) {
  40. std::cout << "ERROR exception" << __LINE__ << std::endl;
  41. assert(false);
  42. }
  43. }
  44. int main()
  45. {
  46. try {
  47. boost::unique_lock<boost::mutex>lk(mut);
  48. boost::thread t(f);
  49. BOOST_TEST(test1 == 0);
  50. while (test1 == 0)
  51. {
  52. cv.wait(lk);
  53. }
  54. BOOST_TEST(test1 != 0);
  55. test2 = 1;
  56. lk.unlock();
  57. cv.notify_one();
  58. t.join();
  59. } catch(...) {
  60. BOOST_TEST(false);
  61. std::cout << "ERROR exception" << __LINE__ << std::endl;
  62. }
  63. return boost::report_errors();
  64. }
  65. #else
  66. #error "Test not applicable: BOOST_THREAD_USES_CHRONO not defined for this platform as not supported"
  67. #endif