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// <mutex> 13 14// class timed_mutex; 15 16// template <class Rep, class Period> 17// unique_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time); 18 19#include <mutex> 20#include <thread> 21#include <cstdlib> 22#include <cassert> 23 24std::timed_mutex m; 25 26typedef std::chrono::steady_clock Clock; 27typedef Clock::time_point time_point; 28typedef Clock::duration duration; 29typedef std::chrono::milliseconds ms; 30typedef std::chrono::nanoseconds ns; 31 32void f1() 33{ 34 time_point t0 = Clock::now(); 35 std::unique_lock<std::timed_mutex> lk(m, ms(300)); 36 assert(lk.owns_lock() == true); 37 time_point t1 = Clock::now(); 38 ns d = t1 - t0 - ms(250); 39 assert(d < ms(50)); // within 50ms 40} 41 42void f2() 43{ 44 time_point t0 = Clock::now(); 45 std::unique_lock<std::timed_mutex> lk(m, ms(250)); 46 assert(lk.owns_lock() == false); 47 time_point t1 = Clock::now(); 48 ns d = t1 - t0 - ms(250); 49 assert(d < ms(50)); // within 50ms 50} 51 52int main() 53{ 54 { 55 m.lock(); 56 std::thread t(f1); 57 std::this_thread::sleep_for(ms(250)); 58 m.unlock(); 59 t.join(); 60 } 61 { 62 m.lock(); 63 std::thread t(f2); 64 std::this_thread::sleep_for(ms(300)); 65 m.unlock(); 66 t.join(); 67 } 68} 69