xref: /llvm-project/libcxx/test/std/thread/thread.condition/thread.condition.condvar/destructor.pass.cpp (revision 7fc6a55688c816f5fc1a5481ae7af25be7500356)
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 #include "test_macros.h"
23 
24 std::condition_variable* cv;
25 std::mutex m;
26 typedef std::unique_lock<std::mutex> Lock;
27 
28 bool f_ready = false;
29 bool g_ready = false;
30 
31 void f()
32 {
33     Lock lk(m);
34     f_ready = true;
35     cv->notify_one();
36     delete cv;
37 }
38 
39 void g()
40 {
41     Lock lk(m);
42     g_ready = true;
43     cv->notify_one();
44     while (!f_ready)
45         cv->wait(lk);
46 }
47 
48 int main(int, char**)
49 {
50     cv = new std::condition_variable;
51     std::thread th2(g);
52     Lock lk(m);
53     while (!g_ready)
54         cv->wait(lk);
55     lk.unlock();
56     std::thread th1(f);
57     th1.join();
58     th2.join();
59 
60   return 0;
61 }
62