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