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 notify_all(); 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 test0 = 0; 26 int test1 = 0; 27 int test2 = 0; 28 29 void f1() 30 { 31 std::unique_lock<std::mutex> lk(mut); 32 assert(test1 == 0); 33 while (test1 == 0) 34 cv.wait(lk); 35 assert(test1 == 1); 36 test1 = 2; 37 } 38 39 void f2() 40 { 41 std::unique_lock<std::mutex> lk(mut); 42 assert(test2 == 0); 43 while (test2 == 0) 44 cv.wait(lk); 45 assert(test2 == 1); 46 test2 = 2; 47 } 48 49 int main(int, char**) 50 { 51 std::thread t1(f1); 52 std::thread t2(f2); 53 std::this_thread::sleep_for(std::chrono::milliseconds(100)); 54 { 55 std::unique_lock<std::mutex>lk(mut); 56 test1 = 1; 57 test2 = 1; 58 } 59 cv.notify_all(); 60 { 61 std::this_thread::sleep_for(std::chrono::milliseconds(100)); 62 std::unique_lock<std::mutex>lk(mut); 63 } 64 t1.join(); 65 t2.join(); 66 assert(test1 == 2); 67 assert(test2 == 2); 68 69 return 0; 70 } 71