• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // template <class Predicate>
16 //   void wait(unique_lock<mutex>& lock, Predicate pred);
17 
18 #include <condition_variable>
19 #include <mutex>
20 #include <thread>
21 #include <functional>
22 #include <cassert>
23 
24 #include "make_test_thread.h"
25 #include "test_macros.h"
26 
27 std::condition_variable cv;
28 std::mutex mut;
29 
30 int test1 = 0;
31 int test2 = 0;
32 
33 class Pred
34 {
35     int& i_;
36 public:
Pred(int & i)37     explicit Pred(int& i) : i_(i) {}
38 
operator ()()39     bool operator()() {return i_ != 0;}
40 };
41 
f()42 void f()
43 {
44     std::unique_lock<std::mutex> lk(mut);
45     assert(test2 == 0);
46     test1 = 1;
47     cv.notify_one();
48     cv.wait(lk, Pred(test2));
49     assert(test2 != 0);
50 }
51 
main(int,char **)52 int main(int, char**)
53 {
54     std::unique_lock<std::mutex>lk(mut);
55     std::thread t = support::make_test_thread(f);
56     assert(test1 == 0);
57     while (test1 == 0)
58         cv.wait(lk);
59     assert(test1 != 0);
60     test2 = 1;
61     lk.unlock();
62     cv.notify_one();
63     t.join();
64 
65   return 0;
66 }
67