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/locks.hpp>
7 
8 // template <class Mutex> class nested_strict_lock;
9 
10 // nested_strict_lock(Mutex &);
11 
12 #include <boost/thread/lock_types.hpp>
13 #include <boost/thread/strict_lock.hpp>
14 #include <boost/thread/mutex.hpp>
15 #include <boost/thread/thread.hpp>
16 #include <boost/detail/lightweight_test.hpp>
17 #include "../../../../timming.hpp"
18 
19 #ifdef BOOST_THREAD_USES_CHRONO
20 typedef boost::chrono::high_resolution_clock Clock;
21 typedef Clock::time_point time_point;
22 typedef Clock::duration duration;
23 typedef boost::chrono::milliseconds ms;
24 typedef boost::chrono::nanoseconds ns;
25 time_point t0;
26 time_point t1;
27 #endif
28 
29 boost::mutex m;
30 
31 const ms max_diff(BOOST_THREAD_TEST_TIME_MS);
32 
f()33 void f()
34 {
35 #ifdef BOOST_THREAD_USES_CHRONO
36   t0 = Clock::now();
37   boost::unique_lock<boost::mutex> lg(m);
38   {
39     boost::nested_strict_lock<boost::unique_lock<boost::mutex> > nlg(lg);
40     t1 = Clock::now();
41   }
42 #else
43   //time_point t0 = Clock::now();
44   //time_point t1;
45   boost::unique_lock<boost::mutex> lg(m);
46   {
47     boost::nested_strict_lock<boost::unique_lock<boost::mutex> > nlg(lg);
48     //t1 = Clock::now();
49   }
50   //ns d = t1 - t0 - ms(250);
51   //BOOST_TEST(d < max_diff);
52 #endif
53 }
54 
main()55 int main()
56 {
57   {
58   m.lock();
59   boost::thread t(f);
60 #ifdef BOOST_THREAD_USES_CHRONO
61   time_point t2 = Clock::now();
62   boost::this_thread::sleep_for(ms(250));
63   time_point t3 = Clock::now();
64 #endif
65   m.unlock();
66   t.join();
67 
68 #if defined BOOST_THREAD_USES_CHRONO
69   ns sleep_time = t3 - t2;
70   ns d_ns = t1 - t0 - sleep_time;
71   ms d_ms = boost::chrono::duration_cast<boost::chrono::milliseconds>(d_ns);
72   // BOOST_TEST_GE(d_ms.count(), 0);
73   BOOST_THREAD_TEST_IT(d_ms, max_diff);
74   BOOST_THREAD_TEST_IT(d_ns, ns(max_diff));
75 #endif
76   }
77 
78   return boost::report_errors();
79 }
80