• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // bool try_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 
24 std::shared_timed_mutex m;
25 
26 typedef std::chrono::system_clock Clock;
27 typedef Clock::time_point time_point;
28 typedef Clock::duration duration;
29 typedef std::chrono::milliseconds ms;
30 typedef std::chrono::nanoseconds ns;
31 
f()32 void f()
33 {
34     time_point t0 = Clock::now();
35     assert(!m.try_lock_shared());
36     assert(!m.try_lock_shared());
37     assert(!m.try_lock_shared());
38     while(!m.try_lock_shared())
39         ;
40     time_point t1 = Clock::now();
41     m.unlock_shared();
42     ns d = t1 - t0 - ms(250);
43     assert(d < ms(200));  // within 200ms
44 }
45 
46 #endif  // _LIBCPP_STD_VER > 11
47 
main()48 int main()
49 {
50 #if _LIBCPP_STD_VER > 11
51     m.lock();
52     std::vector<std::thread> v;
53     for (int i = 0; i < 5; ++i)
54         v.push_back(std::thread(f));
55     std::this_thread::sleep_for(ms(250));
56     m.unlock();
57     for (auto& t : v)
58         t.join();
59 #endif  // _LIBCPP_STD_VER > 11
60 }
61