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 #include "test_macros.h" 25 26 std::mutex m; 27 28 typedef std::chrono::system_clock Clock; 29 typedef Clock::time_point time_point; 30 typedef Clock::duration duration; 31 typedef std::chrono::milliseconds ms; 32 typedef std::chrono::nanoseconds ns; 33 34 void f() 35 { 36 time_point t0 = Clock::now(); 37 time_point t1; 38 { 39 m.lock(); 40 std::lock_guard<std::mutex> lg(m, std::adopt_lock); 41 t1 = Clock::now(); 42 } 43 ns d = t1 - t0 - ms(250); 44 assert(d < ms(50)); // within 50ms 45 } 46 47 int main(int, char**) 48 { 49 m.lock(); 50 std::thread t(f); 51 std::this_thread::sleep_for(ms(250)); 52 m.unlock(); 53 t.join(); 54 55 return 0; 56 } 57