Home | History | Annotate | Download | only in thread.shared_mutex.class
      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 // void lock_shared();
     20 
     21 #include <shared_mutex>
     22 #include <thread>
     23 #include <vector>
     24 #include <cstdlib>
     25 #include <cassert>
     26 
     27 #include "test_macros.h"
     28 
     29 std::shared_mutex m;
     30 
     31 typedef std::chrono::system_clock Clock;
     32 typedef Clock::time_point time_point;
     33 typedef Clock::duration duration;
     34 typedef std::chrono::milliseconds ms;
     35 typedef std::chrono::nanoseconds ns;
     36 
     37 ms WaitTime = ms(250);
     38 
     39 // Thread sanitizer causes more overhead and will sometimes cause this test
     40 // to fail. To prevent this we give Thread sanitizer more time to complete the
     41 // test.
     42 #if !defined(TEST_HAS_SANITIZERS)
     43 ms Tolerance = ms(50);
     44 #else
     45 ms Tolerance = ms(50 * 5);
     46 #endif
     47 
     48 void f()
     49 {
     50     time_point t0 = Clock::now();
     51     m.lock_shared();
     52     time_point t1 = Clock::now();
     53     m.unlock_shared();
     54     ns d = t1 - t0 - WaitTime;
     55     assert(d < Tolerance);  // within tolerance
     56 }
     57 
     58 void g()
     59 {
     60     time_point t0 = Clock::now();
     61     m.lock_shared();
     62     time_point t1 = Clock::now();
     63     m.unlock_shared();
     64     ns d = t1 - t0;
     65     assert(d < Tolerance);  // within tolerance
     66 }
     67 
     68 
     69 int main()
     70 {
     71     m.lock();
     72     std::vector<std::thread> v;
     73     for (int i = 0; i < 5; ++i)
     74         v.push_back(std::thread(f));
     75     std::this_thread::sleep_for(WaitTime);
     76     m.unlock();
     77     for (auto& t : v)
     78         t.join();
     79     m.lock_shared();
     80     for (auto& t : v)
     81         t = std::thread(g);
     82     std::thread q(f);
     83     std::this_thread::sleep_for(WaitTime);
     84     m.unlock_shared();
     85     for (auto& t : v)
     86         t.join();
     87     q.join();
     88 }
     89