• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 lock_guard;
17 
18 // explicit lock_guard(mutex_type& m);
19 
20 // template<class _Mutex> lock_guard(lock_guard<_Mutex>)
21 //     -> lock_guard<_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::lock_guard<std::mutex> lg(m);
44     t1 = Clock::now();
45     }
46     ns d = t1 - t0 - ms(250);
47     assert(d < ms(200));  // within 200ms
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::lock_guard lg(m);
60     static_assert((std::is_same<decltype(lg), std::lock_guard<decltype(m)>>::value), "" );
61 #endif
62 }
63