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// void lock_shared(); 15 16#include <shared_mutex> 17#include <thread> 18#include <vector> 19#include <cstdlib> 20#include <cassert> 21 22#if _LIBCPP_STD_VER > 11 23 24std::shared_timed_mutex m; 25 26typedef std::chrono::system_clock Clock; 27typedef Clock::time_point time_point; 28typedef Clock::duration duration; 29typedef std::chrono::milliseconds ms; 30typedef std::chrono::nanoseconds ns; 31 32void f() 33{ 34 time_point t0 = Clock::now(); 35 m.lock_shared(); 36 time_point t1 = Clock::now(); 37 m.unlock_shared(); 38 ns d = t1 - t0 - ms(250); 39 assert(d < ms(50)); // within 50ms 40} 41 42void g() 43{ 44 time_point t0 = Clock::now(); 45 m.lock_shared(); 46 time_point t1 = Clock::now(); 47 m.unlock_shared(); 48 ns d = t1 - t0; 49 assert(d < ms(50)); // within 50ms 50} 51 52#endif // _LIBCPP_STD_VER > 11 53 54int main() 55{ 56#if _LIBCPP_STD_VER > 11 57 m.lock(); 58 std::vector<std::thread> v; 59 for (int i = 0; i < 5; ++i) 60 v.push_back(std::thread(f)); 61 std::this_thread::sleep_for(ms(250)); 62 m.unlock(); 63 for (auto& t : v) 64 t.join(); 65 m.lock_shared(); 66 for (auto& t : v) 67 t = std::thread(g); 68 std::thread q(f); 69 std::this_thread::sleep_for(ms(250)); 70 m.unlock_shared(); 71 for (auto& t : v) 72 t.join(); 73 q.join(); 74#endif // _LIBCPP_STD_VER > 11 75} 76