1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // UNSUPPORTED: libcpp-has-no-threads
11 
12 // FLAKY_TEST
13 
14 // <mutex>
15 
16 // template <class Mutex> class unique_lock;
17 
18 // explicit unique_lock(mutex_type& m);
19 
20 // template<class _Mutex> unique_lock(unique_lock<_Mutex>)
21 //     -> unique_lock<_Mutex>;  // C++17
22 
23 #include <mutex>
24 #include <thread>
25 #include <cstdlib>
26 #include <cassert>
27 
28 #include "test_macros.h"
29 
30 std::mutex m;
31 
32 typedef std::chrono::system_clock Clock;
33 typedef Clock::time_point time_point;
34 typedef Clock::duration duration;
35 typedef std::chrono::milliseconds ms;
36 typedef std::chrono::nanoseconds ns;
37 
f()38 void f()
39 {
40     time_point t0 = Clock::now();
41     time_point t1;
42     {
43     std::unique_lock<std::mutex> ul(m);
44     t1 = Clock::now();
45     }
46     ns d = t1 - t0 - ms(250);
47     assert(d < ms(50));  // within 50ms
48 }
49 
main()50 int main()
51 {
52     m.lock();
53     std::thread t(f);
54     std::this_thread::sleep_for(ms(250));
55     m.unlock();
56     t.join();
57 
58 #ifdef __cpp_deduction_guides
59     std::unique_lock ul(m);
60     static_assert((std::is_same<decltype(ul), std::unique_lock<decltype(m)>>::value), "" );
61 #endif
62 }
63