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