blob: 6236a13df80e19b4eb7e06db6ae11ac2b722b7dc [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001//===----------------------------------------------------------------------===//
2//
Howard Hinnantf5256e12010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +00009//
10// UNSUPPORTED: libcpp-has-no-threads
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000011
12// <condition_variable>
13
14// class condition_variable;
15
16// void notify_one();
17
18#include <condition_variable>
19#include <mutex>
20#include <thread>
21#include <cassert>
22
23std::condition_variable cv;
24std::mutex mut;
25
Dan Albert1d4a1ed2016-05-25 22:36:09 -070026int test0 = 0;
27int test1 = 0;
28int test2 = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000029
30void f1()
31{
32 std::unique_lock<std::mutex> lk(mut);
33 assert(test1 == 0);
34 while (test1 == 0)
35 cv.wait(lk);
36 assert(test1 == 1);
37 test1 = 2;
38}
39
40void f2()
41{
42 std::unique_lock<std::mutex> lk(mut);
43 assert(test2 == 0);
44 while (test2 == 0)
45 cv.wait(lk);
46 assert(test2 == 1);
47 test2 = 2;
48}
49
50int main()
51{
52 std::thread t1(f1);
53 std::thread t2(f2);
54 std::this_thread::sleep_for(std::chrono::milliseconds(100));
55 {
56 std::unique_lock<std::mutex>lk(mut);
57 test1 = 1;
58 test2 = 1;
59 }
60 cv.notify_one();
61 {
62 std::this_thread::sleep_for(std::chrono::milliseconds(100));
63 std::unique_lock<std::mutex>lk(mut);
64 }
65 if (test1 == 2)
66 {
67 t1.join();
68 test1 = 0;
69 }
70 else if (test2 == 2)
71 {
72 t2.join();
73 test2 = 0;
74 }
75 else
76 assert(false);
77 cv.notify_one();
78 {
79 std::this_thread::sleep_for(std::chrono::milliseconds(100));
80 std::unique_lock<std::mutex>lk(mut);
81 }
82 if (test1 == 2)
83 {
84 t1.join();
85 test1 = 0;
86 }
87 else if (test2 == 2)
88 {
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000089 t2.join();
90 test2 = 0;
91 }
92 else
93 assert(false);
94}