blob: 9c2d8c9c8e5cb7f7f3e0a2ac0fac412d9327b32c [file] [log] [blame]
Howard Hinnantba898e42013-09-21 01:49:28 +00001//===----------------------------------------------------------------------===//
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//===----------------------------------------------------------------------===//
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +00009//
10// UNSUPPORTED: libcpp-has-no-threads
Marshall Clowf54ca462015-06-11 21:47:39 +000011// UNSUPPORTED: c++03, c++98, c++11
Howard Hinnantba898e42013-09-21 01:49:28 +000012
13// <shared_mutex>
14
David Majnemerf9f95be2014-03-17 20:19:44 +000015// class shared_timed_mutex;
Howard Hinnantba898e42013-09-21 01:49:28 +000016
17// bool try_lock_shared();
18
19#include <shared_mutex>
20#include <thread>
21#include <vector>
22#include <cstdlib>
23#include <cassert>
24
David Majnemerf9f95be2014-03-17 20:19:44 +000025std::shared_timed_mutex m;
Howard Hinnantba898e42013-09-21 01:49:28 +000026
27typedef std::chrono::system_clock Clock;
28typedef Clock::time_point time_point;
29typedef Clock::duration duration;
30typedef std::chrono::milliseconds ms;
31typedef std::chrono::nanoseconds ns;
32
33void f()
34{
35 time_point t0 = Clock::now();
36 assert(!m.try_lock_shared());
37 assert(!m.try_lock_shared());
38 assert(!m.try_lock_shared());
39 while(!m.try_lock_shared())
40 ;
41 time_point t1 = Clock::now();
42 m.unlock_shared();
43 ns d = t1 - t0 - ms(250);
Dan Albert1d4a1ed2016-05-25 22:36:09 -070044 assert(d < ms(200)); // within 200ms
Howard Hinnantba898e42013-09-21 01:49:28 +000045}
46
Howard Hinnantba898e42013-09-21 01:49:28 +000047int main()
48{
Howard Hinnantba898e42013-09-21 01:49:28 +000049 m.lock();
50 std::vector<std::thread> v;
51 for (int i = 0; i < 5; ++i)
52 v.push_back(std::thread(f));
53 std::this_thread::sleep_for(ms(250));
54 m.unlock();
55 for (auto& t : v)
56 t.join();
Howard Hinnantba898e42013-09-21 01:49:28 +000057}