1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // <condition_variable>
11 
12 // class condition_variable_any;
13 
14 // template <class Lock, class Rep, class Period, class Predicate>
15 //   bool
16 //   wait_for(Lock& lock, const chrono::duration<Rep, Period>& rel_time,
17 //            Predicate pred);
18 
19 #include <condition_variable>
20 #include <mutex>
21 #include <thread>
22 #include <chrono>
23 #include <cassert>
24 
25 class Pred
26 {
27     int& i_;
28 public:
Pred(int & i)29     explicit Pred(int& i) : i_(i) {}
30 
operator ()()31     bool operator()() {return i_ != 0;}
32 };
33 
34 std::condition_variable_any cv;
35 
36 typedef std::timed_mutex L0;
37 typedef std::unique_lock<L0> L1;
38 
39 L0 m0;
40 
41 int test1 = 0;
42 int test2 = 0;
43 
44 int runs = 0;
45 
f()46 void f()
47 {
48     typedef std::chrono::system_clock Clock;
49     typedef std::chrono::milliseconds milliseconds;
50     L1 lk(m0);
51     assert(test2 == 0);
52     test1 = 1;
53     cv.notify_one();
54     Clock::time_point t0 = Clock::now();
55     bool r = cv.wait_for(lk, milliseconds(250), Pred(test2));
56     Clock::time_point t1 = Clock::now();
57     if (runs == 0)
58     {
59         assert(t1 - t0 < milliseconds(250));
60         assert(test2 != 0);
61     }
62     else
63     {
64         assert(t1 - t0 - milliseconds(250) < milliseconds(50));
65         assert(test2 == 0);
66     }
67     ++runs;
68 }
69 
main()70 int main()
71 {
72     {
73         L1 lk(m0);
74         std::thread t(f);
75         assert(test1 == 0);
76         while (test1 == 0)
77             cv.wait(lk);
78         assert(test1 != 0);
79         test2 = 1;
80         lk.unlock();
81         cv.notify_one();
82         t.join();
83     }
84     test1 = 0;
85     test2 = 0;
86     {
87         L1 lk(m0);
88         std::thread t(f);
89         assert(test1 == 0);
90         while (test1 == 0)
91             cv.wait(lk);
92         assert(test1 != 0);
93         lk.unlock();
94         t.join();
95     }
96 }
97