General Utility Library for C++17 26.5.0
time_util.h
Go to the documentation of this file.
1
26#ifndef GUL17_TIME_UTIL_H_
27#define GUL17_TIME_UTIL_H_
28
29#include <chrono>
30#include <thread>
31
32#include "gul17/internal.h"
33#include "gul17/Trigger.h"
34
35namespace gul17 {
36
57inline std::chrono::steady_clock::time_point tic()
58{
59 return std::chrono::steady_clock::now();
60}
61
94template<class TimeUnitType = std::chrono::duration<double>>
95auto toc(std::chrono::steady_clock::time_point t0)
96{
97 return std::chrono::duration_cast<TimeUnitType>(tic() - t0).count();
98}
99
116template< class Rep, class Period >
117bool sleep(const std::chrono::duration<Rep, Period>& duration, const Trigger& trg)
118{
119 return !trg.wait_for(duration);
120}
121
138inline bool sleep(double seconds, const Trigger &trg)
139{
140 return sleep(std::chrono::duration<double>{ seconds }, trg);
141}
142
161template< class Rep, class Period >
162bool sleep(const std::chrono::duration<Rep, Period>& duration)
163{
164 std::this_thread::sleep_for(duration);
165 return true;
166}
167
185inline bool sleep(double seconds)
186{
187 return sleep(std::chrono::duration<double>{ seconds });
188}
189
191
192} // namespace gul17
193
194#endif
Declaration of the Trigger class for the General Utility Library.
A class that allows sending triggers and waiting for them across different threads.
Definition Trigger.h:114
auto constexpr bit_set(unsigned bit) noexcept -> ReturnT
Set a bit in an integral type.
Definition bit_manip.h:124
auto toc(std::chrono::steady_clock::time_point t0)
Return the elapsed time in seconds (or a different unit) since the given time point.
Definition time_util.h:95
std::chrono::steady_clock::time_point tic()
Return the current time as a std::chrono time_point.
Definition time_util.h:57
bool sleep(const std::chrono::duration< Rep, Period > &duration, const Trigger &trg)
Sleep for at least the given time span, with the option of being woken up from another thread.
Definition time_util.h:117
Definition of macros used internally by GUL.
Namespace gul17 contains all functions and classes of the General Utility Library.
Definition doxygen.h:29