General Utility Library for C++14  2.12
Functions
gul14/time_util.h

Detailed Description

Time-related functions.

Functions

std::chrono::steady_clock::time_point gul14::tic ()
 Return the current time as a std::chrono time_point. More...
 
template<class TimeUnitType = std::chrono::duration<double>>
auto gul14::toc (std::chrono::steady_clock::time_point t0)
 Return the elapsed time in seconds (or a different unit) since the given time point. More...
 
template<class Rep , class Period >
bool gul14::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. More...
 
bool gul14::sleep (double seconds, const Trigger &trg)
 Sleep for a given number of seconds, with the option of being woken up from another thread. More...
 
template<class Rep , class Period >
bool gul14::sleep (const std::chrono::duration< Rep, Period > &duration)
 Sleep for a given time span. More...
 
bool gul14::sleep (double seconds)
 Sleep for a given number of seconds. More...
 

Function Documentation

◆ sleep() [1/4]

template<class Rep , class Period >
bool gul14::sleep ( const std::chrono::duration< Rep, Period > &  duration)

Sleep for a given time span.

This is equivalent to a call of std::this_thread::sleep_for(), which means that control is handed back to the operating system's task scheduler. This may result in a somewhat bigger effective delay than expected, especially where very small sleep times are requested and the system load is high.

Example:

using namespace std::literals; // for the "ms" suffix
sleep(50ms); // Wait 50 milliseconds
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:114
Parameters
durationTime span to wait, as a std::chrono::duration type.
Returns
true to signalize that the entire requested sleep duration has passed. This is for symmetry with the interruptible version of sleep() only.

◆ sleep() [2/4]

template<class Rep , class Period >
bool gul14::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.

The sleep can be interrupted from another thread via a shared Trigger object.

Calling sleep() may lead to a context switch of the operation system. Under heavy load or resource contention, this can produce a delay that is longer than expected.

Parameters
durationTime span to wait, as a std::chrono::duration type.
trgReference to a SleepInterrupt object that can be used to interrupt the delay. If such an interruption occurs, false is returned.
Returns
true if the entire requested sleep duration has passed, or false if the sleep has been interrupted prematurely via the Trigger object.
See also
Trigger
Examples
thread_pool.cc.

References gul14::Trigger::wait_for().

Referenced by gul14::sleep().

◆ sleep() [3/4]

bool gul14::sleep ( double  seconds)
inline

Sleep for a given number of seconds.

This is equivalent to a call of std::this_thread::sleep_for(), which means that control is handed back to the operating system's task scheduler. This may result in a somewhat bigger effective delay than expected, especially where very small sleep times are requested and the system load is high.

Example:

sleep(0.05); // Wait 50 milliseconds
Parameters
secondsSeconds to wait.
Returns
true to signalize that the entire requested sleep duration has passed. This is for symmetry with the interruptible version of sleep() only.

References gul14::sleep().

◆ sleep() [4/4]

bool gul14::sleep ( double  seconds,
const Trigger trg 
)
inline

Sleep for a given number of seconds, with the option of being woken up from another thread.

The sleep can be interrupted from another thread via a shared Trigger object.

Calling sleep() may lead to a context switch of the operation system. Under heavy load or resource contention, this can produce a delay that is longer than expected.

Parameters
secondsSeconds to wait.
trgReference to a SleepInterrupt object that can be used to interrupt the delay. If such an interruption occurs, false is returned.
Returns
true if the entire requested sleep duration has passed, or false if the sleep has been interrupted prematurely via the Trigger object.
See also
Trigger

References gul14::sleep().

◆ tic()

std::chrono::steady_clock::time_point gul14::tic ( )
inline

Return the current time as a std::chrono time_point.

This function is intended to be used with the sister function toc() to measure elapsed time.

Example

auto t0 = tic();
// Do some work
std::cout << "Elapsed time: " << toc(t0) << " seconds.\n";
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:92
std::chrono::steady_clock::time_point tic()
Return the current time as a std::chrono time_point.
Definition: time_util.h:54
See also
toc()

Referenced by gul14::toc().

◆ toc()

template<class TimeUnitType = std::chrono::duration<double>>
auto gul14::toc ( std::chrono::steady_clock::time_point  t0)

Return the elapsed time in seconds (or a different unit) since the given time point.

This function is intended to be used with the sister function tic() to measure elapsed time. toc() is a function template that returns the elapsed seconds as a double value by default; by specifying a different chrono type as a template parameter, it can also return other time units and other types.

Example

auto t0 = tic();
// <do some work>
// Default: Return seconds as a double
std::cout << "Elapsed time: " << toc(t0) << " seconds.\n";
// Custom type: Return milliseconds as an integer
std::cout << "Elapsed time: " << toc<std::chrono::milliseconds>(t0) << " milliseconds.\n";
Template Parameters
TimeUnitTypeThe type to be used for calculating the elapsed time since t0. By default, this is std::chrono::duration<double>, which means that the elapsed time is returned as a double that represents seconds.
Parameters
t0A time point in the past that should be taken with tic().
Returns
the elapsed time in the units and base representation of TimeUnitType. By default, this is a double that represents elapsed seconds. For a TimeUnitType of std::chrono::milliseconds, it would be an integer representing elapsed milliseconds.
See also
tic()

References gul14::tic().