Debian Codegraph @

libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex.pass.cpp

raw
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: no-threads
10 // ALLOW_RETRIES: 2
11
12 // <mutex>
13
14 // template <class Mutex> class unique_lock;
15
16 // explicit unique_lock(mutex_type& m);
17
18 // template<class _Mutex> unique_lock(unique_lock<_Mutex>)
19 // -> unique_lock<_Mutex>; // C++17
20
21 #include <cassert>
22 #include <chrono>
23 #include <cstdlib>
24 #include <mutex>
25 #include <thread>
26
27 #include "make_test_thread.h"
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
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
50 int main(int, char**)
51 {
52 m.lock();
53 std::thread t = support::make_test_thread(f);
54 std::this_thread::sleep_for(ms(250));
55 m.unlock();
56 t.join();
57
58 #if TEST_STD_VER >= 17
59 std::unique_lock ul(m);
60 static_assert((std::is_same<decltype(ul), std::unique_lock<decltype(m)>>::value), "" );
61 #endif
62
63 return 0;
64 }