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