1*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===// 2*0a6a1f1dSLionel Sambuc // 3*0a6a1f1dSLionel Sambuc // The LLVM Compiler Infrastructure 4*0a6a1f1dSLionel Sambuc // 5*0a6a1f1dSLionel Sambuc // This file is dual licensed under the MIT and the University of Illinois Open 6*0a6a1f1dSLionel Sambuc // Source Licenses. See LICENSE.TXT for details. 7*0a6a1f1dSLionel Sambuc // 8*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===// 9*0a6a1f1dSLionel Sambuc // 10*0a6a1f1dSLionel Sambuc // UNSUPPORTED: libcpp-has-no-threads 11*0a6a1f1dSLionel Sambuc 12*0a6a1f1dSLionel Sambuc // <mutex> 13*0a6a1f1dSLionel Sambuc 14*0a6a1f1dSLionel Sambuc // class timed_mutex; 15*0a6a1f1dSLionel Sambuc 16*0a6a1f1dSLionel Sambuc // void lock(); 17*0a6a1f1dSLionel Sambuc 18*0a6a1f1dSLionel Sambuc #include <mutex> 19*0a6a1f1dSLionel Sambuc #include <thread> 20*0a6a1f1dSLionel Sambuc #include <cstdlib> 21*0a6a1f1dSLionel Sambuc #include <cassert> 22*0a6a1f1dSLionel Sambuc 23*0a6a1f1dSLionel Sambuc #include <iostream> 24*0a6a1f1dSLionel Sambuc 25*0a6a1f1dSLionel Sambuc std::timed_mutex m; 26*0a6a1f1dSLionel Sambuc 27*0a6a1f1dSLionel Sambuc typedef std::chrono::system_clock Clock; 28*0a6a1f1dSLionel Sambuc typedef Clock::time_point time_point; 29*0a6a1f1dSLionel Sambuc typedef Clock::duration duration; 30*0a6a1f1dSLionel Sambuc typedef std::chrono::milliseconds ms; 31*0a6a1f1dSLionel Sambuc typedef std::chrono::nanoseconds ns; 32*0a6a1f1dSLionel Sambuc f()33*0a6a1f1dSLionel Sambucvoid f() 34*0a6a1f1dSLionel Sambuc { 35*0a6a1f1dSLionel Sambuc time_point t0 = Clock::now(); 36*0a6a1f1dSLionel Sambuc m.lock(); 37*0a6a1f1dSLionel Sambuc time_point t1 = Clock::now(); 38*0a6a1f1dSLionel Sambuc m.unlock(); 39*0a6a1f1dSLionel Sambuc ns d = t1 - t0 - ms(250); 40*0a6a1f1dSLionel Sambuc assert(d < ms(50)); // within 50ms 41*0a6a1f1dSLionel Sambuc } 42*0a6a1f1dSLionel Sambuc main()43*0a6a1f1dSLionel Sambucint main() 44*0a6a1f1dSLionel Sambuc { 45*0a6a1f1dSLionel Sambuc m.lock(); 46*0a6a1f1dSLionel Sambuc std::thread t(f); 47*0a6a1f1dSLionel Sambuc std::this_thread::sleep_for(ms(250)); 48*0a6a1f1dSLionel Sambuc m.unlock(); 49*0a6a1f1dSLionel Sambuc t.join(); 50*0a6a1f1dSLionel Sambuc } 51