General Utility Library for C++14  2.8
time_util.h
Go to the documentation of this file.
1 
23 #ifndef GUL14_TIME_UTIL_H_
24 #define GUL14_TIME_UTIL_H_
25 
26 #include <chrono>
27 #include <thread>
28 
29 #include "gul14/internal.h"
30 #include "gul14/Trigger.h"
31 
32 namespace gul14 {
33 
34 
49 inline std::chrono::steady_clock::time_point tic()
50 {
51  return std::chrono::steady_clock::now();
52 }
53 
86 template<class TimeUnitType = std::chrono::duration<double>>
87 auto toc(std::chrono::steady_clock::time_point t0)
88 {
89  return std::chrono::duration_cast<TimeUnitType>(tic() - t0).count();
90 }
91 
108 template< class Rep, class Period >
109 bool sleep(const std::chrono::duration<Rep, Period>& duration, const Trigger& trg)
110 {
111  return !trg.wait_for(duration);
112 }
113 
130 inline bool sleep(double seconds, const Trigger &trg)
131 {
132  return sleep(std::chrono::duration<double>{ seconds }, trg);
133 }
134 
153 template< class Rep, class Period >
154 bool sleep(const std::chrono::duration<Rep, Period>& duration)
155 {
156  std::this_thread::sleep_for(duration);
157  return true;
158 }
159 
177 inline bool sleep(double seconds)
178 {
179  return sleep(std::chrono::duration<double>{ seconds });
180 }
181 
182 } // namespace gul14
183 
184 #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:105
bool wait_for(const std::chrono::duration< Rep, Period > &delta_t) const
Suspend execution of the current thread until the trigger goes high (true) or at least the given time...
Definition: Trigger.h:183
Definition of macros used internally by GUL.
Namespace gul14 contains all functions and classes of the General Utility Library.
Definition: doxygen.h:26
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:87
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:109
std::chrono::steady_clock::time_point tic()
Return the current time as a std::chrono time_point.
Definition: time_util.h:49