Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / thread / test / sync / mutual_exclusion / locks / strict_lock / make_strict_lock_pass.cpp
1 // Copyright (C) 2011 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/strict_lock.hpp>
7
8 // template <class Lockable>
9 // strict_lock<Lockable> make_strict_lock(Lockable &);
10
11 #define BOOST_THREAD_VERSION 4
12
13 #include <boost/thread/strict_lock.hpp>
14 #include <boost/thread/mutex.hpp>
15 #include <boost/thread/thread.hpp>
16
17 #include <boost/detail/lightweight_test.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 #endif
26
27 boost::mutex m;
28
29 #if ! defined(BOOST_NO_CXX11_AUTO_DECLARATIONS) && ! defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && ! defined BOOST_THREAD_NO_MAKE_STRICT_LOCK && defined BOOST_THREAD_USES_CHRONO
30
31 void f()
32 {
33   time_point t0 = Clock::now();
34   time_point t1;
35   {
36     const auto&& lg = boost::make_strict_lock(m); (void)lg;
37     t1 = Clock::now();
38   }
39   ns d = t1 - t0 - ms(250);
40   // This test is spurious as it depends on the time the thread system switches the threads
41   BOOST_TEST(d < ns(2500000)+ms(1000)); // within 2.5ms
42 }
43 #endif
44
45 int main()
46 {
47
48 #if ! defined(BOOST_NO_CXX11_AUTO_DECLARATIONS) && ! defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && ! defined BOOST_THREAD_NO_MAKE_STRICT_LOCK
49   {
50     m.lock();
51     boost::thread t(f);
52     boost::this_thread::sleep_for(ms(250));
53     m.unlock();
54     t.join();
55   }
56 #endif
57   return boost::report_errors();
58 }