blob: a34207406411a714d32d79b54fe2cdf6b080f971 [file] [log] [blame]
Howard Hinnant3e519522010-05-11 19:42:16 +00001//===----------------------------------------------------------------------===//
2//
Chandler Carruth57b08b02019-01-19 10:56:40 +00003// 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
Howard Hinnant3e519522010-05-11 19:42:16 +00006//
7//===----------------------------------------------------------------------===//
Jonathan Roelofsb3fcc672014-09-05 19:45:05 +00008//
9// UNSUPPORTED: libcpp-has-no-threads
Howard Hinnant3e519522010-05-11 19:42:16 +000010
11// <condition_variable>
12
13// class condition_variable;
14
15// void wait(unique_lock<mutex>& lock);
16
17#include <condition_variable>
18#include <mutex>
19#include <thread>
20#include <cassert>
21
22std::condition_variable cv;
23std::mutex mut;
24
25int test1 = 0;
26int test2 = 0;
27
28void f()
29{
30 std::unique_lock<std::mutex> lk(mut);
31 assert(test2 == 0);
32 test1 = 1;
33 cv.notify_one();
34 while (test2 == 0)
35 cv.wait(lk);
36 assert(test2 != 0);
37}
38
39int main()
40{
41 std::unique_lock<std::mutex>lk(mut);
42 std::thread t(f);
43 assert(test1 == 0);
44 while (test1 == 0)
45 cv.wait(lk);
46 assert(test1 != 0);
47 test2 = 1;
48 lk.unlock();
49 cv.notify_one();
50 t.join();
51}