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 // void wait(unique_lock<mutex>& lock); 16 17 #include <condition_variable> 18 #include <mutex> 19 #include <thread> 20 #include <cassert> 21 22 std::condition_variable cv; 23 std::mutex mut; 24 25 int test1 = 0; 26 int test2 = 0; 27 28 void f() 29 { 30 std::unique_lock<std::mutex> lk(mut); 31 assert(test2 == 0); 32 test1 = 1; 33 cv.notify_one(); 34 while (test2 == 0) 35 cv.wait(lk); 36 assert(test2 != 0); 37 } 38 39 int main(int, char**) 40 { 41 std::unique_lock<std::mutex>lk(mut); 42 std::thread t(f); 43 assert(test1 == 0); 44 while (test1 == 0) 45 cv.wait(lk); 46 assert(test1 != 0); 47 test2 = 1; 48 lk.unlock(); 49 cv.notify_one(); 50 t.join(); 51 52 return 0; 53 } 54