MSTL 1.4.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
clocks.hpp
浏览该文件的文档.
1#ifndef MSTL_CORE_TIME_CLOCKS_HPP__
2#define MSTL_CORE_TIME_CLOCKS_HPP__
3
10
13
19
20struct MSTL_API steady_clock;
21
22
29struct MSTL_API system_clock {
34
35 static_assert(system_clock::duration::min() < system_clock::duration::zero(),
36 "a clock's minimum duration cannot be less than its epoch");
37
43 static constexpr bool is_steady = false;
44
51 static time_point now() noexcept;
52
58 static seconds to_seconds(const time_point& time_point_value) noexcept {
59 return time_point_value.since_epoch().to_sec();
60 }
61
67 static time_point from_seconds(const seconds time_value) noexcept {
69 return time_cast<system_clock::duration>(TP(time_value));
70 }
71};
72
73
81struct MSTL_API steady_clock {
86
92 static constexpr bool is_steady = true;
93
98 static time_point now() noexcept;
99
105 template <typename Dur>
108 const auto steady_now = steady_clock::now();
109 const auto sys_now = system_clock::now();
110
111 const auto diff = tp - steady_now;
112 return sys_now + _MSTL time_cast<system_clock::duration>(diff);
113 }
114};
115
131milliseconds MSTL_API relative_time(int64_t sec, int64_t nsec, bool is_monotonic = false);
132
133
139template <typename T>
140struct is_clock;
141
142template <>
143struct is_clock<system_clock> : true_type {};
144
145template <>
146struct is_clock<steady_clock> : true_type {};
147
152template <typename T>
153MSTL_INLINE17 constexpr bool is_clock_v = is_clock<T>::value;
154 // Clocks
156
158
164
175template <typename Clock, typename Dur>
177 auto current = Clock::now();
178 if (Clock::is_steady) {
179 if (current < time) {
180 _THIS_THREAD sleep_for(time - current);
181 }
182 return;
183 }
184 while (current < time) {
185 _THIS_THREAD sleep_for(time - current);
186 current = Clock::now();
187 }
188}
189 // Thread
191
193
195#endif // MSTL_CORE_TIME_CLOCKS_HPP__
MSTL_INLINE17 constexpr bool is_clock_v
is_clock的便捷变量模板
milliseconds MSTL_API relative_time(int64_t sec, int64_t nsec, bool is_monotonic=false)
将绝对时间戳转换为相对延迟毫秒数
long long int64_t
64位有符号整数类型
duration< int64_t, milli > milliseconds
毫秒持续时间
duration< int64_t, nano > nanoseconds
纳秒持续时间
constexpr ToDur time_cast(const duration< Rep, Period > &value)
持续时间类型转换
duration< int64_t > seconds
秒持续时间
#define _MSTL
全局命名空间MSTL前缀
#define MSTL_BEGIN_THIS_THREAD__
this_thread命名空间
#define _THIS_THREAD
this_thread命名空间前缀
#define MSTL_END_THIS_THREAD__
结束this_thread命名空间
#define MSTL_END_NAMESPACE__
结束全局命名空间MSTL
#define MSTL_BEGIN_NAMESPACE__
开始全局命名空间MSTL
void sleep_until(const time_point< Clock, Dur > &time)
使当前线程睡眠直到指定时间点
void sleep_for(const duration< Rep, Period > time)
使当前线程睡眠指定时间
bool_constant< true > true_type
表示true的类型
typename Period::type period
时间单位比例类型
Rep rep
数值类型
检查是否为时钟类型
稳定时钟
duration::period period
时间单位比例
static time_point now() noexcept
获取当前时间点
static system_clock::time_point to_system(const _MSTL time_point< steady_clock, Dur > &tp)
将稳定时钟转为系统时钟
_MSTL nanoseconds duration
持续时间类型
static constexpr bool is_steady
时钟稳定性标识
duration::rep rep
数值类型
_MSTL time_point< steady_clock > time_point
时间点类型
系统时钟
_MSTL time_point< system_clock > time_point
时间点类型
static constexpr bool is_steady
时钟稳定性标识
static time_point now() noexcept
获取当前时间点
duration::period period
时间单位比例
duration::rep rep
数值类型
_MSTL nanoseconds duration
持续时间类型
static seconds to_seconds(const time_point &time_point_value) noexcept
将时间点转换为时间值
static time_point from_seconds(const seconds time_value) noexcept
从时间值创建时间点
时间点类模板
MSTL时间点类型