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 // Copyright (C) 2011 Vicente J. Botet Escriba
11 //
12 //  Distributed under the Boost Software License, Version 1.0. (See accompanying
13 //  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
14 
15 // <boost/thread/mutex.hpp>
16 
17 // class mutex;
18 
19 // void lock();
20 
21 #include <boost/thread/mutex.hpp>
22 #include <boost/thread/thread.hpp>
23 #include <boost/detail/lightweight_test.hpp>
24 
25 
26 boost::mutex m;
27 
28 #if defined BOOST_THREAD_USES_CHRONO
29 typedef boost::chrono::system_clock Clock;
30 typedef Clock::time_point time_point;
31 typedef Clock::duration duration;
32 typedef boost::chrono::milliseconds ms;
33 typedef boost::chrono::nanoseconds ns;
34 #else
35 #endif
36 
f()37 void f()
38 {
39 #if defined BOOST_THREAD_USES_CHRONO
40   time_point t0 = Clock::now();
41   m.lock();
42   time_point t1 = Clock::now();
43   m.unlock();
44   ns d = t1 - t0 - ms(250);
45   // This test is spurious as it depends on the time the thread system switches the threads
46   BOOST_TEST(d < ns(2500000)+ms(1000)); // within 2.5ms
47 #else
48   //time_point t0 = Clock::now();
49   m.lock();
50   //time_point t1 = Clock::now();
51   m.unlock();
52   //ns d = t1 - t0 - ms(250);
53   // This test is spurious as it depends on the time the thread system switches the threads
54   //BOOST_TEST(d < ns(2500000)+ms(1000)); // within 2.5ms
55 #endif
56 }
57 
main()58 int main()
59 {
60   m.lock();
61   boost::thread t(f);
62 #if defined BOOST_THREAD_USES_CHRONO
63   boost::this_thread::sleep_for(ms(250));
64 #endif
65   m.unlock();
66   t.join();
67 
68   return boost::report_errors();
69 }
70 
71 
72