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 // Copyright (C) 2011 Vicente J. Botet Escriba
10 //
11 //  Distributed under the Boost Software License, Version 1.0. (See accompanying
12 //  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
13 
14 // <boost/thread/thread.hpp>
15 
16 // thread::id this_thread::get_id();
17 
18 #include <boost/thread/thread_only.hpp>
19 #include <cstdlib>
20 #include <algorithm>
21 
22 #include <boost/detail/lightweight_test.hpp>
23 
24 #if defined BOOST_THREAD_USES_CHRONO
25 
main()26 int main()
27 {
28   {
29     typedef boost::chrono::steady_clock Clock;
30     typedef Clock::time_point time_point;
31     //typedef Clock::duration duration;
32     boost::chrono::milliseconds ms(500);
33     time_point t0 = Clock::now();
34     boost::this_thread::sleep_until(t0 + ms);
35     time_point t1 = Clock::now();
36     boost::chrono::nanoseconds ns = (t1 - t0) - ms;
37     boost::chrono::nanoseconds err = ms / 100;
38     // The time slept is within 1% of 500ms
39     // This test is spurious as it depends on the time the thread system switches the threads
40     BOOST_TEST((std::max)(ns.count(), -ns.count()) < (err+boost::chrono::milliseconds(1000)).count());
41     //BOOST_TEST(std::abs(static_cast<long>(ns.count())) < (err+boost::chrono::milliseconds(1000)).count());
42   }
43   {
44     typedef boost::chrono::system_clock Clock;
45     typedef Clock::time_point time_point;
46     //typedef Clock::duration duration;
47     boost::chrono::milliseconds ms(500);
48     time_point t0 = Clock::now();
49     boost::this_thread::sleep_until(t0 + ms);
50     time_point t1 = Clock::now();
51     boost::chrono::nanoseconds ns = (t1 - t0) - ms;
52     boost::chrono::nanoseconds err = ms / 100;
53     // The time slept is within 1% of 500ms
54     // This test is spurious as it depends on the time the thread system switches the threads
55     BOOST_TEST((std::max)(ns.count(), -ns.count()) < (err+boost::chrono::milliseconds(1000)).count());
56     //BOOST_TEST(std::abs(static_cast<long>(ns.count())) < (err+boost::chrono::milliseconds(1000)).count());
57   }
58   {
59     typedef boost::chrono::steady_clock Clock;
60     typedef Clock::time_point time_point;
61     //typedef Clock::duration duration;
62     boost::chrono::milliseconds ms(500);
63     time_point t0 = Clock::now();
64     boost::this_thread::no_interruption_point::sleep_until(t0 + ms);
65     time_point t1 = Clock::now();
66     boost::chrono::nanoseconds ns = (t1 - t0) - ms;
67     boost::chrono::nanoseconds err = ms / 100;
68     // The time slept is within 1% of 500ms
69     // This test is spurious as it depends on the time the thread system switches the threads
70     BOOST_TEST((std::max)(ns.count(), -ns.count()) < (err+boost::chrono::milliseconds(1000)).count());
71     //BOOST_TEST(std::abs(static_cast<long>(ns.count())) < (err+boost::chrono::milliseconds(1000)).count());
72   }
73   {
74     typedef boost::chrono::system_clock Clock;
75     typedef Clock::time_point time_point;
76     //typedef Clock::duration duration;
77     boost::chrono::milliseconds ms(500);
78     time_point t0 = Clock::now();
79     boost::this_thread::no_interruption_point::sleep_until(t0 + ms);
80     time_point t1 = Clock::now();
81     boost::chrono::nanoseconds ns = (t1 - t0) - ms;
82     boost::chrono::nanoseconds err = ms / 100;
83     // The time slept is within 1% of 500ms
84     // This test is spurious as it depends on the time the thread system switches the threads
85     BOOST_TEST((std::max)(ns.count(), -ns.count()) < (err+boost::chrono::milliseconds(1000)).count());
86     //BOOST_TEST(std::abs(static_cast<long>(ns.count())) < (err+boost::chrono::milliseconds(1000)).count());
87   }
88   return boost::report_errors();
89 
90 }
91 
92 #else
93 #error "Test not applicable: BOOST_THREAD_USES_CHRONO not defined for this platform as not supported"
94 #endif
95 
96