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// <shared_mutex> 11 12// class shared_timed_mutex; 13 14// template <class Rep, class Period> 15// bool try_lock_for(const chrono::duration<Rep, Period>& rel_time); 16 17#include <shared_mutex> 18#include <thread> 19#include <cstdlib> 20#include <cassert> 21 22#if _LIBCPP_STD_VER > 11 23 24std::shared_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 assert(m.try_lock_for(ms(300)) == true); 36 time_point t1 = Clock::now(); 37 m.unlock(); 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 assert(m.try_lock_for(ms(250)) == false); 46 time_point t1 = Clock::now(); 47 ns d = t1 - t0 - ms(250); 48 assert(d < ms(50)); // within 50ms 49} 50 51#endif // _LIBCPP_STD_VER > 11 52 53int main() 54{ 55#if _LIBCPP_STD_VER > 11 56 { 57 m.lock(); 58 std::thread t(f1); 59 std::this_thread::sleep_for(ms(250)); 60 m.unlock(); 61 t.join(); 62 } 63 { 64 m.lock(); 65 std::thread t(f2); 66 std::this_thread::sleep_for(ms(300)); 67 m.unlock(); 68 t.join(); 69 } 70#endif // _LIBCPP_STD_VER > 11 71} 72