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 <iostream> 25 #include <cassert> 26 #include "../../../timming.hpp" 27 28 #if defined BOOST_THREAD_USES_CHRONO 29 typedef boost::chrono::milliseconds ms; 30 typedef boost::chrono::nanoseconds ns; 31 struct Clock 32 { 33 typedef boost::chrono::milliseconds duration; 34 typedef duration::rep rep; 35 typedef duration::period period; 36 typedef boost::chrono::time_point<Clock> time_point; 37 static const bool is_steady = true; 38 nowClock39 static time_point now() 40 { 41 using namespace boost::chrono; 42 return time_point(duration_cast<duration> (steady_clock::now().time_since_epoch())); 43 } 44 }; 45 46 boost::condition_variable cv; 47 boost::mutex mut; 48 49 50 int test1 = 0; 51 int test2 = 0; 52 53 int runs = 0; 54 55 const ms max_diff(BOOST_THREAD_TEST_TIME_MS); 56 f()57void f() 58 { 59 try { 60 boost::unique_lock < boost::mutex > lk(mut); 61 assert(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 while (test2 == 0 && cv.wait_until(lk, t) == boost::cv_status::no_timeout) {} 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 assert(test2 != 0); 73 } 74 else 75 { 76 ns d = t1 - t0 - Clock::duration(250); 77 BOOST_THREAD_TEST_IT(d, ns(max_diff)); 78 assert(test2 == 0); 79 } 80 ++runs; 81 } catch(...) { 82 assert(false); 83 std::cout << "ERROR exception" << __LINE__ << std::endl; 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 106 test1 = 0; 107 test2 = 0; 108 try 109 { 110 boost::unique_lock < boost::mutex > lk(mut); 111 boost::thread t(f); 112 BOOST_TEST(test1 == 0); 113 while (test1 == 0) 114 cv.wait(lk); 115 BOOST_TEST(test1 != 0); 116 lk.unlock(); 117 t.join(); 118 } catch(...) { 119 BOOST_TEST(false); 120 std::cout << "ERROR exception" << __LINE__ << std::endl; 121 } 122 123 return boost::report_errors(); 124 } 125 126 #else 127 #error "Test not applicable: BOOST_THREAD_USES_CHRONO not defined for this platform as not supported" 128 #endif 129