1 // Copyright (C) 2012 Vicente J. Botet Escriba 2 // 3 // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 5 6 // <boost/thread/lock_factories.hpp> 7 8 // template <class Mutex> 9 // unique_lock<Mutex> make_unique_lock(Mutex&); 10 11 #define BOOST_THREAD_VERSION 4 12 13 #include <boost/thread/detail/config.hpp> 14 #include <boost/thread/lock_factories.hpp> 15 #include <boost/thread/mutex.hpp> 16 #include <boost/thread/thread.hpp> 17 #include <boost/detail/lightweight_test.hpp> 18 #include "../../../../../timming.hpp" 19 20 //#if ! defined(BOOST_NO_CXX11_RVALUE_REFERENCES) 21 22 boost::mutex m; 23 24 #if defined BOOST_THREAD_USES_CHRONO 25 26 typedef boost::chrono::high_resolution_clock Clock; 27 typedef Clock::time_point time_point; 28 typedef Clock::duration duration; 29 typedef boost::chrono::milliseconds ms; 30 typedef boost::chrono::nanoseconds ns; 31 time_point t0; 32 time_point t1; 33 #else 34 #endif 35 36 const ms max_diff(BOOST_THREAD_TEST_TIME_MS); 37 f()38void f() 39 { 40 #if defined BOOST_THREAD_USES_CHRONO 41 t0 = Clock::now(); 42 { 43 #if ! defined(BOOST_NO_CXX11_AUTO_DECLARATIONS) 44 auto 45 #else 46 boost::unique_lock<boost::mutex> 47 #endif 48 //&& 49 _ = boost::make_unique_lock(m); (void)_; 50 t1 = Clock::now(); 51 } 52 #else 53 //time_point t0 = Clock::now(); 54 //time_point t1; 55 { 56 #if ! defined(BOOST_NO_CXX11_AUTO_DECLARATIONS) 57 auto 58 #else 59 boost::unique_lock<boost::mutex> 60 #endif 61 //&& 62 _ = boost::make_unique_lock(m); (void)_; 63 //t1 = Clock::now(); 64 } 65 //ns d = t1 - t0 - ms(250); 66 //BOOST_TEST(d < max_diff); 67 #endif 68 } 69 main()70int main() 71 { 72 m.lock(); 73 boost::thread t(f); 74 #if defined BOOST_THREAD_USES_CHRONO 75 time_point t2 = Clock::now(); 76 boost::this_thread::sleep_for(ms(250)); 77 time_point t3 = Clock::now(); 78 #else 79 #endif 80 m.unlock(); 81 t.join(); 82 83 #if defined BOOST_THREAD_USES_CHRONO 84 ns sleep_time = t3 - t2; 85 ns d_ns = t1 - t0 - sleep_time; 86 ms d_ms = boost::chrono::duration_cast<boost::chrono::milliseconds>(d_ns); 87 // BOOST_TEST_GE(d_ms.count(), 0); 88 BOOST_THREAD_TEST_IT(d_ms, max_diff); 89 BOOST_THREAD_TEST_IT(d_ns, ns(max_diff)); 90 #endif 91 92 return boost::report_errors(); 93 } 94 //#else 95 //int main() 96 //{ 97 // return boost::report_errors(); 98 //} 99 //#endif 100 101