14684ddb6SLionel Sambuc //===----------------------------------------------------------------------===//
24684ddb6SLionel Sambuc //
34684ddb6SLionel Sambuc //                     The LLVM Compiler Infrastructure
44684ddb6SLionel Sambuc //
54684ddb6SLionel Sambuc // This file is dual licensed under the MIT and the University of Illinois Open
64684ddb6SLionel Sambuc // Source Licenses. See LICENSE.TXT for details.
74684ddb6SLionel Sambuc //
84684ddb6SLionel Sambuc //===----------------------------------------------------------------------===//
94684ddb6SLionel Sambuc 
104684ddb6SLionel Sambuc // <shared_mutex>
114684ddb6SLionel Sambuc 
124684ddb6SLionel Sambuc // template <class Mutex> class shared_lock;
134684ddb6SLionel Sambuc 
144684ddb6SLionel Sambuc // explicit shared_lock(mutex_type& m);
154684ddb6SLionel Sambuc 
164684ddb6SLionel Sambuc #include <shared_mutex>
174684ddb6SLionel Sambuc #include <thread>
184684ddb6SLionel Sambuc #include <vector>
194684ddb6SLionel Sambuc #include <cstdlib>
204684ddb6SLionel Sambuc #include <cassert>
214684ddb6SLionel Sambuc 
224684ddb6SLionel Sambuc #if _LIBCPP_STD_VER > 11
234684ddb6SLionel Sambuc 
24*0a6a1f1dSLionel Sambuc std::shared_timed_mutex m;
254684ddb6SLionel Sambuc 
264684ddb6SLionel Sambuc typedef std::chrono::system_clock Clock;
274684ddb6SLionel Sambuc typedef Clock::time_point time_point;
284684ddb6SLionel Sambuc typedef Clock::duration duration;
294684ddb6SLionel Sambuc typedef std::chrono::milliseconds ms;
304684ddb6SLionel Sambuc typedef std::chrono::nanoseconds ns;
314684ddb6SLionel Sambuc 
f()324684ddb6SLionel Sambuc void f()
334684ddb6SLionel Sambuc {
344684ddb6SLionel Sambuc     time_point t0 = Clock::now();
354684ddb6SLionel Sambuc     time_point t1;
364684ddb6SLionel Sambuc     {
37*0a6a1f1dSLionel Sambuc     std::shared_lock<std::shared_timed_mutex> ul(m);
384684ddb6SLionel Sambuc     t1 = Clock::now();
394684ddb6SLionel Sambuc     }
404684ddb6SLionel Sambuc     ns d = t1 - t0 - ms(250);
414684ddb6SLionel Sambuc     assert(d < ms(50));  // within 50ms
424684ddb6SLionel Sambuc }
434684ddb6SLionel Sambuc 
g()444684ddb6SLionel Sambuc void g()
454684ddb6SLionel Sambuc {
464684ddb6SLionel Sambuc     time_point t0 = Clock::now();
474684ddb6SLionel Sambuc     time_point t1;
484684ddb6SLionel Sambuc     {
49*0a6a1f1dSLionel Sambuc     std::shared_lock<std::shared_timed_mutex> ul(m);
504684ddb6SLionel Sambuc     t1 = Clock::now();
514684ddb6SLionel Sambuc     }
524684ddb6SLionel Sambuc     ns d = t1 - t0;
534684ddb6SLionel Sambuc     assert(d < ms(50));  // within 50ms
544684ddb6SLionel Sambuc }
554684ddb6SLionel Sambuc 
564684ddb6SLionel Sambuc #endif  // _LIBCPP_STD_VER > 11
574684ddb6SLionel Sambuc 
main()584684ddb6SLionel Sambuc int main()
594684ddb6SLionel Sambuc {
604684ddb6SLionel Sambuc #if _LIBCPP_STD_VER > 11
614684ddb6SLionel Sambuc     m.lock();
624684ddb6SLionel Sambuc     std::vector<std::thread> v;
634684ddb6SLionel Sambuc     for (int i = 0; i < 5; ++i)
644684ddb6SLionel Sambuc         v.push_back(std::thread(f));
654684ddb6SLionel Sambuc     std::this_thread::sleep_for(ms(250));
664684ddb6SLionel Sambuc     m.unlock();
674684ddb6SLionel Sambuc     for (auto& t : v)
684684ddb6SLionel Sambuc         t.join();
694684ddb6SLionel Sambuc     m.lock_shared();
704684ddb6SLionel Sambuc     for (auto& t : v)
714684ddb6SLionel Sambuc         t = std::thread(g);
724684ddb6SLionel Sambuc     std::thread q(f);
734684ddb6SLionel Sambuc     std::this_thread::sleep_for(ms(250));
744684ddb6SLionel Sambuc     m.unlock_shared();
754684ddb6SLionel Sambuc     for (auto& t : v)
764684ddb6SLionel Sambuc         t.join();
774684ddb6SLionel Sambuc     q.join();
784684ddb6SLionel Sambuc #endif  // _LIBCPP_STD_VER > 11
794684ddb6SLionel Sambuc }
80