1 //===----------------------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // UNSUPPORTED: libcpp-has-no-threads 10 11 // <condition_variable> 12 13 // class condition_variable; 14 15 // ~condition_variable(); 16 17 #include <condition_variable> 18 #include <mutex> 19 #include <thread> 20 #include <cassert> 21 22 std::condition_variable* cv; 23 std::mutex m; 24 typedef std::unique_lock<std::mutex> Lock; 25 26 bool f_ready = false; 27 bool g_ready = false; 28 29 void f() 30 { 31 Lock lk(m); 32 f_ready = true; 33 cv->notify_one(); 34 delete cv; 35 } 36 37 void g() 38 { 39 Lock lk(m); 40 g_ready = true; 41 cv->notify_one(); 42 while (!f_ready) 43 cv->wait(lk); 44 } 45 46 int main(int, char**) 47 { 48 cv = new std::condition_variable; 49 std::thread th2(g); 50 Lock lk(m); 51 while (!g_ready) 52 cv->wait(lk); 53 lk.unlock(); 54 std::thread th1(f); 55 th1.join(); 56 th2.join(); 57 58 return 0; 59 } 60