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