xref: /llvm-project/libcxx/test/std/thread/thread.condition/notify_all_at_thread_exit.pass.cpp (revision 564628014c404bf57aa7cd9a5337198046bdd1ed)
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 // notify_all_at_thread_exit(...) requires move semantics to transfer the
12 // unique_lock.
13 // UNSUPPORTED: c++03
14 
15 // <condition_variable>
16 
17 // void
18 //   notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk);
19 
20 #include <condition_variable>
21 #include <mutex>
22 #include <thread>
23 #include <chrono>
24 #include <cassert>
25 
26 #include "make_test_thread.h"
27 #include "test_macros.h"
28 
29 std::condition_variable cv;
30 std::mutex mut;
31 
32 typedef std::chrono::milliseconds ms;
33 typedef std::chrono::high_resolution_clock Clock;
34 
35 void func()
36 {
37     std::unique_lock<std::mutex> lk(mut);
38     std::notify_all_at_thread_exit(cv, std::move(lk));
39     std::this_thread::sleep_for(ms(300));
40 }
41 
42 int main(int, char**)
43 {
44     std::unique_lock<std::mutex> lk(mut);
45     std::thread t = support::make_test_thread(func);
46     Clock::time_point t0 = Clock::now();
47     cv.wait(lk);
48     Clock::time_point t1 = Clock::now();
49     assert(t1-t0 > ms(250));
50     t.join();
51 
52   return 0;
53 }
54