]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 | ||
17 | #ifdef BOOST_THREAD_USES_CHRONO | |
18 | typedef boost::chrono::high_resolution_clock Clock; | |
19 | typedef Clock::time_point time_point; | |
20 | typedef Clock::duration duration; | |
21 | typedef boost::chrono::milliseconds ms; | |
22 | typedef boost::chrono::nanoseconds ns; | |
23 | #endif | |
24 | ||
25 | boost::mutex m; | |
26 | ||
27 | void f() | |
28 | { | |
29 | #ifdef BOOST_THREAD_USES_CHRONO | |
30 | time_point t0 = Clock::now(); | |
31 | time_point t1; | |
32 | { | |
33 | boost::strict_lock<boost::mutex> lg(m); | |
34 | t1 = Clock::now(); | |
35 | } | |
36 | ns d = t1 - t0 - ms(250); | |
37 | // This test is spurious as it depends on the time the thread system switches the threads | |
38 | BOOST_TEST(d < ns(2500000)+ms(1000)); // within 2.5ms | |
39 | #else | |
40 | //time_point t0 = Clock::now(); | |
41 | //time_point t1; | |
42 | { | |
43 | boost::strict_lock<boost::mutex> lg(m); | |
44 | //t1 = Clock::now(); | |
45 | } | |
46 | //ns d = t1 - t0 - ms(250); | |
47 | // This test is spurious as it depends on the time the thread system switches the threads | |
48 | //BOOST_TEST(d < ns(2500000)+ms(1000)); // within 2.5ms | |
49 | #endif | |
50 | } | |
51 | ||
52 | int main() | |
53 | { | |
54 | m.lock(); | |
55 | boost::thread t(f); | |
56 | #ifdef BOOST_THREAD_USES_CHRONO | |
57 | boost::this_thread::sleep_for(ms(250)); | |
58 | #endif | |
59 | m.unlock(); | |
60 | t.join(); | |
61 | ||
62 | return boost::report_errors(); | |
63 | } |