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 // UNSUPPORTED: c++03, c++98, c++11 12 13 // <shared_mutex> 14 15 // class shared_timed_mutex; 16 17 // bool try_lock_shared(); 18 19 #include <shared_mutex> 20 #include <thread> 21 #include <vector> 22 #include <cstdlib> 23 #include <cassert> 24 25 #include "test_macros.h" 26 27 std::shared_timed_mutex m; 28 29 typedef std::chrono::system_clock Clock; 30 typedef Clock::time_point time_point; 31 typedef Clock::duration duration; 32 typedef std::chrono::milliseconds ms; 33 typedef std::chrono::nanoseconds ns; 34 35 36 #if !defined(TEST_HAS_SANITIZERS) 37 ms Tolerance = ms(200); 38 #else 39 ms Tolerance = ms(200 * 5); 40 #endif 41 42 void f() 43 { 44 time_point t0 = Clock::now(); 45 assert(!m.try_lock_shared()); 46 assert(!m.try_lock_shared()); 47 assert(!m.try_lock_shared()); 48 while(!m.try_lock_shared()) 49 ; 50 time_point t1 = Clock::now(); 51 m.unlock_shared(); 52 ns d = t1 - t0 - ms(250); 53 assert(d < Tolerance); // within tolerance 54 } 55 56 int main() 57 { 58 m.lock(); 59 std::vector<std::thread> v; 60 for (int i = 0; i < 5; ++i) 61 v.push_back(std::thread(f)); 62 std::this_thread::sleep_for(ms(250)); 63 m.unlock(); 64 for (auto& t : v) 65 t.join(); 66 } 67