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