xref: /llvm-project/libcxx/test/std/thread/thread.condition/notify_all_at_thread_exit.pass.cpp (revision a55632a069d89804938d7ef33192888f539b3bc8)
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 // TODO(ldionne): This test fails on Ubuntu Focal on our CI nodes (and only there), in 32 bit mode.
16 // UNSUPPORTED: linux && 32bits-on-64bits
17 
18 // <condition_variable>
19 
20 // void
21 //   notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk);
22 
23 #include <condition_variable>
24 #include <mutex>
25 #include <thread>
26 #include <chrono>
27 #include <cassert>
28 
29 #include "make_test_thread.h"
30 #include "test_macros.h"
31 
32 std::condition_variable cv;
33 std::mutex mut;
34 
35 typedef std::chrono::milliseconds ms;
36 typedef std::chrono::high_resolution_clock Clock;
37 
38 void func()
39 {
40     std::unique_lock<std::mutex> lk(mut);
41     std::notify_all_at_thread_exit(cv, std::move(lk));
42     std::this_thread::sleep_for(ms(300));
43 }
44 
45 int main(int, char**)
46 {
47     std::unique_lock<std::mutex> lk(mut);
48     std::thread t = support::make_test_thread(func);
49     Clock::time_point t0 = Clock::now();
50     cv.wait(lk);
51     Clock::time_point t1 = Clock::now();
52     assert(t1-t0 > ms(250));
53     t.join();
54 
55   return 0;
56 }
57