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 // FLAKY_TEST.
12 
13 // <mutex>
14 
15 // template <class Mutex> class lock_guard;
16 
17 // lock_guard(mutex_type& m, adopt_lock_t);
18 
19 #include <mutex>
20 #include <thread>
21 #include <cstdlib>
22 #include <cassert>
23 
24 std::mutex m;
25 
26 typedef std::chrono::system_clock Clock;
27 typedef Clock::time_point time_point;
28 typedef Clock::duration duration;
29 typedef std::chrono::milliseconds ms;
30 typedef std::chrono::nanoseconds ns;
31 
32 void f()
33 {
34     time_point t0 = Clock::now();
35     time_point t1;
36     {
37     m.lock();
38     std::lock_guard<std::mutex> lg(m, std::adopt_lock);
39     t1 = Clock::now();
40     }
41     ns d = t1 - t0 - ms(250);
42     assert(d < ms(50));  // within 50ms
43 }
44 
45 int main(int, char**)
46 {
47     m.lock();
48     std::thread t(f);
49     std::this_thread::sleep_for(ms(250));
50     m.unlock();
51     t.join();
52 
53   return 0;
54 }
55