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