Home | History | Annotate | Download | only in thread.lock.unique.cons
      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 unique_lock;
     15 
     16 // explicit unique_lock(mutex_type& m);
     17 
     18 // template<class _Mutex> unique_lock(unique_lock<_Mutex>)
     19 //     -> unique_lock<_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::unique_lock<std::mutex> ul(m);
     42     t1 = Clock::now();
     43     }
     44     ns d = t1 - t0 - ms(250);
     45     assert(d < ms(50));  // within 50ms
     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::unique_lock ul(m);
     58     static_assert((std::is_same<decltype(ul), std::unique_lock<decltype(m)>>::value), "" );
     59 #endif
     60 }
     61