Home | History | Annotate | Download | only in thread.lock.guard
      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 
     12 // <mutex>
     13 
     14 // template <class Mutex> class lock_guard;
     15 
     16 // explicit lock_guard(mutex_type& m);
     17 
     18 // template<class _Mutex> lock_guard(lock_guard<_Mutex>)
     19 //     -> lock_guard<_Mutex>;  // C++17
     20 
     21 #include <mutex>
     22 #include <thread>
     23 #include <cstdlib>
     24 #include <cassert>
     25 
     26 #include "test_macros.h"
     27 
     28 std::mutex m;
     29 
     30 typedef std::chrono::system_clock Clock;
     31 typedef Clock::time_point time_point;
     32 typedef Clock::duration duration;
     33 typedef std::chrono::milliseconds ms;
     34 typedef std::chrono::nanoseconds ns;
     35 
     36 void f()
     37 {
     38     time_point t0 = Clock::now();
     39     time_point t1;
     40     {
     41     std::lock_guard<std::mutex> lg(m);
     42     t1 = Clock::now();
     43     }
     44     ns d = t1 - t0 - ms(250);
     45     assert(d < ms(200));  // within 200ms
     46 }
     47 
     48 int main()
     49 {
     50     m.lock();
     51     std::thread t(f);
     52     std::this_thread::sleep_for(ms(250));
     53     m.unlock();
     54     t.join();
     55 
     56 #ifdef __cpp_deduction_guides
     57     std::lock_guard lg(m);
     58     static_assert((std::is_same<decltype(lg), std::lock_guard<decltype(m)>>::value), "" );
     59 #endif
     60 }
     61