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, c++14 12 13 // FLAKY_TEST. 14 15 // <shared_mutex> 16 17 // class shared_mutex; 18 19 // void lock(); 20 21 #include <shared_mutex> 22 #include <thread> 23 #include <cstdlib> 24 #include <cassert> 25 26 #include "test_macros.h" 27 28 std::shared_mutex m; 29 30 typedef std::chrono::system_clock Clock; 31 typedef Clock::time_point time_point; 32 typedef Clock::duration duration; 33 typedef std::chrono::milliseconds ms; 34 typedef std::chrono::nanoseconds ns; 35 36 ms WaitTime = ms(250); 37 38 // Thread sanitizer causes more overhead and will sometimes cause this test 39 // to fail. To prevent this we give Thread sanitizer more time to complete the 40 // test. 41 #if !defined(TEST_HAS_SANITIZERS) 42 ms Tolerance = ms(50); 43 #else 44 ms Tolerance = ms(50 * 5); 45 #endif 46 47 void f() 48 { 49 time_point t0 = Clock::now(); 50 m.lock(); 51 time_point t1 = Clock::now(); 52 m.unlock(); 53 ns d = t1 - t0 - WaitTime; 54 assert(d < Tolerance); // within tolerance 55 } 56 57 int main() 58 { 59 m.lock(); 60 std::thread t(f); 61 std::this_thread::sleep_for(WaitTime); 62 m.unlock(); 63 t.join(); 64 } 65