1 // Distributed under the Boost Software License, Version 1.0. (See
2 // accompanying file LICENSE_1_0.txt or copy at
3 // http://www.boost.org/LICENSE_1_0.txt)
4 // (C) Copyright 2011 Vicente J. Botet Escriba
6 #ifndef BOOST_THREAD_V2_THREAD_HPP
7 #define BOOST_THREAD_V2_THREAD_HPP
9 #include <boost/thread/detail/config.hpp>
10 #ifdef BOOST_THREAD_USES_CHRONO
11 #include <boost/chrono/system_clocks.hpp>
12 #include <boost/chrono/ceil.hpp>
14 #include <boost/thread/condition_variable.hpp>
15 #include <boost/thread/lock_types.hpp>
21 namespace no_interruption_point
23 #ifdef BOOST_THREAD_USES_CHRONO
25 template <class Clock, class Duration>
26 void sleep_until(const chrono::time_point<Clock, Duration>& t)
28 using namespace chrono;
30 condition_variable cv;
31 unique_lock<mutex> lk(mut);
32 while (Clock::now() < t)
36 #ifdef BOOST_THREAD_SLEEP_FOR_IS_STEADY
38 template <class Rep, class Period>
39 void sleep_for(const chrono::duration<Rep, Period>& d)
41 using namespace chrono;
42 if (d > duration<Rep, Period>::zero())
44 duration<long double> Max = nanoseconds::max BOOST_PREVENT_MACRO_SUBSTITUTION ();
48 ns = duration_cast<nanoseconds>(d);
53 ns = nanoseconds:: max BOOST_PREVENT_MACRO_SUBSTITUTION ();
58 template <class Duration>
59 inline BOOST_SYMBOL_VISIBLE
60 void sleep_until(const chrono::time_point<chrono::steady_clock, Duration>& t)
62 using namespace chrono;
63 sleep_for(t - steady_clock::now());
66 template <class Rep, class Period>
67 void sleep_for(const chrono::duration<Rep, Period>& d)
69 using namespace chrono;
70 if (d > duration<Rep, Period>::zero())
72 steady_clock::time_point c_timeout = steady_clock::now() + ceil<nanoseconds>(d);
73 sleep_until(c_timeout);
81 #ifdef BOOST_THREAD_USES_CHRONO
83 template <class Clock, class Duration>
84 void sleep_until(const chrono::time_point<Clock, Duration>& t)
86 using namespace chrono;
88 condition_variable cv;
89 unique_lock<mutex> lk(mut);
90 while (Clock::now() < t)
94 #if defined BOOST_THREAD_HAS_CONDATTR_SET_CLOCK_MONOTONIC && defined BOOST_CHRONO_HAS_CLOCK_STEADY
95 template <class Rep, class Period>
96 void sleep_for(const chrono::duration<Rep, Period>& d)
98 using namespace chrono;
99 if (d > duration<Rep, Period>::zero())
101 steady_clock::time_point c_timeout = steady_clock::now() + ceil<nanoseconds>(d);
102 sleep_until(c_timeout);
106 #elif defined BOOST_THREAD_SLEEP_FOR_IS_STEADY
108 template <class Rep, class Period>
109 void sleep_for(const chrono::duration<Rep, Period>& d)
111 using namespace chrono;
112 if (d > duration<Rep, Period>::zero())
114 duration<long double> Max = nanoseconds::max BOOST_PREVENT_MACRO_SUBSTITUTION ();
118 ns = duration_cast<nanoseconds>(d);
123 ns = nanoseconds:: max BOOST_PREVENT_MACRO_SUBSTITUTION ();
128 template <class Duration>
129 inline BOOST_SYMBOL_VISIBLE
130 void sleep_until(const chrono::time_point<chrono::steady_clock, Duration>& t)
132 using namespace chrono;
133 sleep_for(t - steady_clock::now());
136 template <class Rep, class Period>
137 void sleep_for(const chrono::duration<Rep, Period>& d)
139 using namespace chrono;
140 if (d > duration<Rep, Period>::zero())
142 //system_clock::time_point c_timeout = time_point_cast<system_clock::duration>(system_clock::now() + ceil<nanoseconds>(d));
143 system_clock::time_point c_timeout = system_clock::now() + ceil<system_clock::duration>(d);
144 sleep_until(c_timeout);