]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/std/thread/thread.mutex/thread.mutex.requirements/thread.shared_mutex.requirements/thread.shared_mutex.class/try_lock_shared.pass.cpp
Vendor import of libc++ trunk r290819:
[FreeBSD/FreeBSD.git] / test / std / thread / thread.mutex / thread.mutex.requirements / thread.shared_mutex.requirements / thread.shared_mutex.class / try_lock_shared.pass.cpp
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 // bool try_lock_shared();
20
21 #include <shared_mutex>
22 #include <thread>
23 #include <vector>
24 #include <cstdlib>
25 #include <cassert>
26
27 std::shared_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 void f()
36 {
37     time_point t0 = Clock::now();
38     assert(!m.try_lock_shared());
39     assert(!m.try_lock_shared());
40     assert(!m.try_lock_shared());
41     while(!m.try_lock_shared())
42         ;
43     time_point t1 = Clock::now();
44     m.unlock_shared();
45     ns d = t1 - t0 - ms(250);
46     assert(d < ms(200));  // within 200ms
47 }
48
49
50 int main()
51 {
52     m.lock();
53     std::vector<std::thread> v;
54     for (int i = 0; i < 5; ++i)
55         v.push_back(std::thread(f));
56     std::this_thread::sleep_for(ms(250));
57     m.unlock();
58     for (auto& t : v)
59         t.join();
60 }