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/thread/locks.hpp> 24 #include <boost/detail/lightweight_test.hpp> 25 26 boost::condition_variable* cv; 27 boost::mutex m; 28 typedef boost::unique_lock<boost::mutex> Lock; 29 30 bool f_ready = false; 31 bool g_ready = false; 32 f()33void f() 34 { 35 Lock lk(m); 36 f_ready = true; 37 cv->notify_one(); 38 cv->wait(lk); 39 delete cv; 40 } 41 g()42void g() 43 { 44 Lock lk(m); 45 g_ready = true; 46 cv->notify_one(); 47 while (!f_ready) 48 { 49 cv->wait(lk); 50 } 51 cv->notify_one(); 52 } 53 main()54int main() 55 { 56 cv = new boost::condition_variable; 57 boost::thread th2(g); 58 Lock lk(m); 59 while (!g_ready) 60 { 61 cv->wait(lk); 62 } 63 lk.unlock(); 64 boost::thread th1(f); 65 th1.join(); 66 th2.join(); 67 return boost::report_errors(); 68 } 69 70