1 //===----------------------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // UNSUPPORTED: libcpp-has-no-threads
16 // template <class Rep, class Period>
17 // shared_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time);
19 #include <shared_mutex>
25 #if _LIBCPP_STD_VER > 11
27 std::shared_timed_mutex m;
29 typedef std::chrono::steady_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;
37 time_point t0 = Clock::now();
38 std::shared_lock<std::shared_timed_mutex> lk(m, ms(300));
39 assert(lk.owns_lock() == true);
40 time_point t1 = Clock::now();
41 ns d = t1 - t0 - ms(250);
42 assert(d < ms(50)); // within 50ms
47 time_point t0 = Clock::now();
48 std::shared_lock<std::shared_timed_mutex> lk(m, ms(250));
49 assert(lk.owns_lock() == false);
50 time_point t1 = Clock::now();
51 ns d = t1 - t0 - ms(250);
52 assert(d < ms(50)); // within 50ms
55 #endif // _LIBCPP_STD_VER > 11
59 #if _LIBCPP_STD_VER > 11
62 std::vector<std::thread> v;
63 for (int i = 0; i < 5; ++i)
64 v.push_back(std::thread(f1));
65 std::this_thread::sleep_for(ms(250));
72 std::vector<std::thread> v;
73 for (int i = 0; i < 5; ++i)
74 v.push_back(std::thread(f2));
75 std::this_thread::sleep_for(ms(300));
80 #endif // _LIBCPP_STD_VER > 11