jaulib v1.1.2-28-g30efb27-dirty
Jau Support Library (C++, Java, ..)
Namespaces | Classes | Typedefs | Functions
Fraction Arithmetic and Time

Fraction type and arithmetic support inclusive its utilization for time without loss of precision nor range. More...

Namespaces

namespace  jau::fraction_tv
 
namespace  jau::fractions_i64
 fractions namespace to provide fraction constants using int64_t as underlying integral integer type.
 
namespace  jau::fractions_i64_literals
 

Classes

class  jau::fraction< Int_type, >
 Fraction template type using integral values, evaluated at runtime. More...
 
struct  jau::fraction_timespec
 Timespec structure using int64_t for its components in analogy to struct timespec_t on 64-bit platforms. More...
 

Typedefs

typedef fraction< int64_t > jau::fraction_i64
 fraction using int64_t as integral type More...
 
typedef fraction< uint64_t > jau::fraction_u64
 fraction using uint64_t as integral type More...
 
typedef ordered_atomic< jau::fraction_i64, std::memory_order_relaxed > jau::relaxed_atomic_fraction_i64
 Relaxed non-SC atomic integral scalar jau::fraction_i64. More...
 
typedef ordered_atomic< jau::fraction_u64, std::memory_order_relaxed > jau::relaxed_atomic_fraction_u64
 Relaxed non-SC atomic integral scalar jau::fraction_u64. More...
 
typedef ordered_atomic< jau::fraction_i64, std::memory_order_seq_cst > jau::sc_atomic_fraction_i64
 SC atomic integral scalar jau::fraction_i64. More...
 
typedef ordered_atomic< jau::fraction_u64, std::memory_order_seq_cst > jau::sc_atomic_fraction_u64
 SC atomic integral scalar jau::fraction_u64. More...
 

Functions

template<typename int_type >
constexpr fraction< int_type > jau::abs (const fraction< int_type > &rhs) noexcept
 Returns the absolute fraction. More...
 
constexpr fraction_timespec jau::abs (const fraction_timespec &rhs) noexcept
 Returns the absolute fraction_timespec. More...
 
fraction_timespec jau::getMonotonicTime () noexcept
 Returns current monotonic time since Unix Epoch 00:00:00 UTC on 1970-01-01. More...
 
fraction_timespec jau::getWallClockTime () noexcept
 Returns current wall-clock real-time since Unix Epoch 00:00:00 UTC on 1970-01-01. More...
 
template<typename int_type >
constexpr const fraction< int_type > & jau::max (const fraction< int_type > &lhs, const fraction< int_type > &rhs) noexcept
 Return the maximum of the two given fractions. More...
 
constexpr const fraction_timespecjau::max (const fraction_timespec &lhs, const fraction_timespec &rhs) noexcept
 Return the maximum of the two given fraction_timespec. More...
 
template<typename int_type >
constexpr const fraction< int_type > & jau::min (const fraction< int_type > &lhs, const fraction< int_type > &rhs) noexcept
 Return the minimum of the two given fractions. More...
 
constexpr const fraction_timespecjau::min (const fraction_timespec &lhs, const fraction_timespec &rhs) noexcept
 Return the minimum of the two given fraction_timespec. More...
 
template<typename int_type >
constexpr bool jau::operator!= (const fraction< int_type > &lhs, const fraction< int_type > &rhs) noexcept
 
constexpr bool jau::operator!= (const fraction_timespec &lhs, const fraction_timespec &rhs) noexcept
 
template<typename int_type >
constexpr fraction< int_type > jau::operator* (const fraction< int_type > &lhs, const fraction< int_type > &rhs) noexcept
 Returns product of two fraction. More...
 
template<typename int_type >
constexpr fraction< int_type > jau::operator* (const fraction< int_type > &lhs, const int_type &rhs) noexcept
 Returns multiplication of fraction with scalar. More...
 
template<typename int_type >
constexpr fraction< int_type > jau::operator* (const int_type &lhs, const fraction< int_type > &rhs) noexcept
 Returns multiplication of fraction with scalar. More...
 
template<typename int_type >
constexpr fraction< int_type > jau::operator+ (const fraction< int_type > &lhs, const fraction< int_type > &rhs) noexcept
 Returns sum of two fraction. More...
 
constexpr fraction_timespec jau::operator+ (const fraction_timespec &lhs, const fraction_timespec &rhs) noexcept
 Returns sum of two fraction_timespec. More...
 
template<typename int_type >
constexpr fraction< int_type > jau::operator- (const fraction< int_type > &lhs, const fraction< int_type > &rhs) noexcept
 Returns difference of two fraction. More...
 
constexpr fraction_timespec jau::operator- (const fraction_timespec &lhs, const fraction_timespec &rhs) noexcept
 Returns difference of two fraction_timespec. More...
 
template<typename int_type >
constexpr fraction< int_type > jau::operator/ (const fraction< int_type > &lhs, const fraction< int_type > &rhs) noexcept
 Returns division of two fraction. More...
 
template<typename int_type >
constexpr fraction< int_type > jau::operator/ (const fraction< int_type > &lhs, const int_type &rhs) noexcept
 Returns division of fraction with scalar. More...
 
template<typename int_type >
constexpr fraction< int_type > jau::operator/ (const int_type &lhs, const fraction< int_type > &rhs) noexcept
 Returns division of fraction with scalar. More...
 
template<typename int_type >
constexpr bool jau::operator< (const fraction< int_type > &lhs, const fraction< int_type > &rhs) noexcept
 
constexpr bool jau::operator< (const fraction_timespec &lhs, const fraction_timespec &rhs) noexcept
 
template<typename int_type >
constexpr bool jau::operator<= (const fraction< int_type > &lhs, const fraction< int_type > &rhs) noexcept
 
constexpr bool jau::operator<= (const fraction_timespec &lhs, const fraction_timespec &rhs) noexcept
 
template<typename int_type >
constexpr bool jau::operator== (const fraction< int_type > &lhs, const fraction< int_type > &rhs) noexcept
 
constexpr bool jau::operator== (const fraction_timespec &lhs, const fraction_timespec &rhs) noexcept
 
template<typename int_type >
constexpr bool jau::operator> (const fraction< int_type > &lhs, const fraction< int_type > &rhs) noexcept
 
constexpr bool jau::operator> (const fraction_timespec &lhs, const fraction_timespec &rhs) noexcept
 
template<typename int_type >
constexpr bool jau::operator>= (const fraction< int_type > &lhs, const fraction< int_type > &rhs) noexcept
 
constexpr bool jau::operator>= (const fraction_timespec &lhs, const fraction_timespec &rhs) noexcept
 
template<typename int_type >
constexpr snsize_t jau::sign (const fraction< int_type > &rhs) noexcept
 Returns the value of the sign function applied to numerator. More...
 
constexpr snsize_t jau::sign (const fraction_timespec &rhs) noexcept
 Returns the value of the sign function applied to tv_sec. More...
 
void jau::sleep_for (const fraction_i64 &relative_time, const bool monotonic=true) noexcept
 sleep_for causes the current thread to block until a specific amount of time has passed. More...
 
void jau::sleep_for (const fraction_timespec &relative_time, const bool monotonic=true) noexcept
 sleep_for causes the current thread to block until a specific amount of time has passed. More...
 
void jau::sleep_until (const fraction_timespec &absolute_time, const bool monotonic=true) noexcept
 sleep_until causes the current thread to block until the specific time is reached. More...
 
bool jau::to_fraction_i64 (fraction_i64 &result, const std::string &value, const fraction_i64 &min_allowed, const fraction_i64 &max_allowed) noexcept
 Stores the fraction_i64 value of the given string value in format <num>/<denom>, which may contain whitespace. More...
 
template<typename int_type >
std::string jau::to_string (const fraction< int_type > &v) noexcept
 
std::string jau::to_string (const fraction_timespec &v) noexcept
 
std::cv_status jau::wait_for (std::condition_variable &cv, std::unique_lock< std::mutex > &lock, const fraction_i64 &relative_time, const bool monotonic=true) noexcept
 wait_for causes the current thread to block until the condition variable is notified, a specific amount of time has passed, or a spurious wakeup occurs. More...
 
std::cv_status jau::wait_for (std::condition_variable &cv, std::unique_lock< std::mutex > &lock, const fraction_timespec &relative_time, const bool monotonic=true) noexcept
 wait_for causes the current thread to block until the condition variable is notified, a specific amount of time has passed, or a spurious wakeup occurs. More...
 
std::cv_status jau::wait_until (std::condition_variable &cv, std::unique_lock< std::mutex > &lock, const fraction_timespec &absolute_time, const bool monotonic=true) noexcept
 wait_until causes the current thread to block until the condition variable is notified, a specific time is reached, or a spurious wakeup occurs. More...
 

Detailed Description

Fraction type and arithmetic support inclusive its utilization for time without loss of precision nor range.

Adds support for Concurrency with general time functionality like sleep_until(), sleep_for(), wait_until() and wait_for(), completed with getMonotonicTime() and getWallClockTime().

Typedef Documentation

◆ fraction_i64

typedef fraction<int64_t> jau::fraction_i64

fraction using int64_t as integral type

Definition at line 739 of file fraction_type.hpp.

◆ fraction_u64

typedef fraction<uint64_t> jau::fraction_u64

fraction using uint64_t as integral type

Definition at line 761 of file fraction_type.hpp.

◆ sc_atomic_fraction_i64

typedef ordered_atomic<jau::fraction_i64, std::memory_order_seq_cst> jau::sc_atomic_fraction_i64

SC atomic integral scalar jau::fraction_i64.

Memory-Model (MM) guaranteed sequential consistency (SC) between acquire (read) and release (write). Requires libatomic with libstdc++10.

Definition at line 1281 of file fraction_type.hpp.

◆ relaxed_atomic_fraction_i64

Relaxed non-SC atomic integral scalar jau::fraction_i64.

Memory-Model (MM) only guarantees the atomic value, no sequential consistency (SC) between acquire (read) and release (write). Requires libatomic with libstdc++10.

Definition at line 1284 of file fraction_type.hpp.

◆ sc_atomic_fraction_u64

typedef ordered_atomic<jau::fraction_u64, std::memory_order_seq_cst> jau::sc_atomic_fraction_u64

SC atomic integral scalar jau::fraction_u64.

Memory-Model (MM) guaranteed sequential consistency (SC) between acquire (read) and release (write). Requires libatomic with libstdc++10.

Definition at line 1287 of file fraction_type.hpp.

◆ relaxed_atomic_fraction_u64

Relaxed non-SC atomic integral scalar jau::fraction_u64.

Memory-Model (MM) only guarantees the atomic value, no sequential consistency (SC) between acquire (read) and release (write). Requires libatomic with libstdc++10.

Definition at line 1290 of file fraction_type.hpp.

Function Documentation

◆ getMonotonicTime()

fraction_timespec jau::getMonotonicTime ( )
noexcept

Returns current monotonic time since Unix Epoch 00:00:00 UTC on 1970-01-01.

See http://man7.org/linux/man-pages/man2/clock_gettime.2.html

Returned fraction_timespec is passing machine precision and range of the underlying API.

See fraction_timespec::to_fraction_i64() of how to measure duration in high range and precision:

  fraction_timespec t0 = getMonotonicTime();
  // do something

  // Exact duration
  fraction_timespec td_1 = getMonotonicTime() - t0;

  // or for durations <= 292 years
  fraction_i64 td_2 = (getMonotonicTime() - t0).to_fraction_i64();

This is in stark contract to counting nanoseconds in int64_t which only lasts until 2262-04-12, since INT64_MAX is 9'223'372'036'854'775'807 for 9'223'372'036 seconds or 292 years.

Monotonic time shall be used for high-performance measurements of durations, since the underlying OS shall support fast calls.

See also
fraction_timespec
fraction_timespec::to_fraction_i64()
getWallClockTime()

Regarding avoiding kernel via VDSO, see http://man7.org/linux/man-pages/man7/vdso.7.html, clock_gettime seems to be well supported at least on kernel >= 4.4. Only bfin and sh are missing, while ia64 seems to be complicated.

Definition at line 51 of file basic_types.cpp.

Here is the caller graph for this function:

◆ getWallClockTime()

fraction_timespec jau::getWallClockTime ( )
noexcept

Returns current wall-clock real-time since Unix Epoch 00:00:00 UTC on 1970-01-01.

Returned fraction_timespec is passing machine precision and range of the underlying API.

Wall-Clock time shall be used for accurate measurements of the actual time only, since the underlying OS unlikely supports fast calls.

See also
fraction_timespec
fraction_timespec::to_fraction_i64()
getMonotonicTime()

Definition at line 57 of file basic_types.cpp.

Here is the caller graph for this function:

◆ to_string() [1/2]

template<typename int_type >
std::string jau::to_string ( const fraction< int_type > &  v)
inlinenoexcept

Definition at line 549 of file fraction_type.hpp.

◆ operator!=() [1/2]

template<typename int_type >
constexpr bool jau::operator!= ( const fraction< int_type > &  lhs,
const fraction< int_type > &  rhs 
)
constexprnoexcept

Definition at line 552 of file fraction_type.hpp.

◆ operator==() [1/2]

template<typename int_type >
constexpr bool jau::operator== ( const fraction< int_type > &  lhs,
const fraction< int_type > &  rhs 
)
constexprnoexcept

Definition at line 557 of file fraction_type.hpp.

◆ operator>() [1/2]

template<typename int_type >
constexpr bool jau::operator> ( const fraction< int_type > &  lhs,
const fraction< int_type > &  rhs 
)
constexprnoexcept

Definition at line 562 of file fraction_type.hpp.

◆ operator>=() [1/2]

template<typename int_type >
constexpr bool jau::operator>= ( const fraction< int_type > &  lhs,
const fraction< int_type > &  rhs 
)
constexprnoexcept

Definition at line 567 of file fraction_type.hpp.

◆ operator<() [1/2]

template<typename int_type >
constexpr bool jau::operator< ( const fraction< int_type > &  lhs,
const fraction< int_type > &  rhs 
)
constexprnoexcept

Definition at line 572 of file fraction_type.hpp.

◆ operator<=() [1/2]

template<typename int_type >
constexpr bool jau::operator<= ( const fraction< int_type > &  lhs,
const fraction< int_type > &  rhs 
)
constexprnoexcept

Definition at line 577 of file fraction_type.hpp.

◆ max() [1/2]

template<typename int_type >
constexpr const fraction< int_type > & jau::max ( const fraction< int_type > &  lhs,
const fraction< int_type > &  rhs 
)
constexprnoexcept

Return the maximum of the two given fractions.

Examples
test_intdecstring01.cpp.

Definition at line 583 of file fraction_type.hpp.

Here is the caller graph for this function:

◆ min() [1/2]

template<typename int_type >
constexpr const fraction< int_type > & jau::min ( const fraction< int_type > &  lhs,
const fraction< int_type > &  rhs 
)
constexprnoexcept

Return the minimum of the two given fractions.

Examples
test_intdecstring01.cpp, test_mm_sc_drf_00.cpp, and test_mm_sc_drf_01.cpp.

Definition at line 589 of file fraction_type.hpp.

Here is the caller graph for this function:

◆ sign() [1/2]

template<typename int_type >
constexpr snsize_t jau::sign ( const fraction< int_type > &  rhs)
constexprnoexcept

Returns the value of the sign function applied to numerator.

-1 for numerator < 0
 0 for numerator = 0
 1 for numerator > 0
Returns
function result

Definition at line 603 of file fraction_type.hpp.

Here is the caller graph for this function:

◆ abs() [1/2]

template<typename int_type >
constexpr fraction< int_type > jau::abs ( const fraction< int_type > &  rhs)
constexprnoexcept

Returns the absolute fraction.

Definition at line 611 of file fraction_type.hpp.

Here is the caller graph for this function:

◆ operator*() [1/3]

template<typename int_type >
constexpr fraction< int_type > jau::operator* ( const fraction< int_type > &  lhs,
const int_type &  rhs 
)
constexprnoexcept

Returns multiplication of fraction with scalar.

Operation may set the overflow flag in the returned instance, if occurring.

Template Parameters
int_typeintegral type
Parameters
lhsthe fraction
rhsthe scalar
Returns
resulting new fraction, reduced

Definition at line 628 of file fraction_type.hpp.

◆ operator*() [2/3]

template<typename int_type >
constexpr fraction< int_type > jau::operator* ( const int_type &  lhs,
const fraction< int_type > &  rhs 
)
constexprnoexcept

Returns multiplication of fraction with scalar.

Operation may set the overflow flag in the returned instance, if occurring.

Template Parameters
int_typeintegral type
Parameters
lhsthe scalar
rhsthe fraction
Returns
resulting new fraction, reduced

Definition at line 643 of file fraction_type.hpp.

◆ operator/() [1/3]

template<typename int_type >
constexpr fraction< int_type > jau::operator/ ( const fraction< int_type > &  lhs,
const int_type &  rhs 
)
constexprnoexcept

Returns division of fraction with scalar.

Template Parameters
int_typeintegral type
Parameters
lhsthe fraction
rhsthe scalar
Returns
resulting new fraction, reduced

Definition at line 655 of file fraction_type.hpp.

◆ operator/() [2/3]

template<typename int_type >
constexpr fraction< int_type > jau::operator/ ( const int_type &  lhs,
const fraction< int_type > &  rhs 
)
constexprnoexcept

Returns division of fraction with scalar.

Template Parameters
int_typeintegral type
Parameters
lhsthe scalar
rhsthe fraction
Returns
resulting new fraction, reduced

Definition at line 668 of file fraction_type.hpp.

◆ operator+() [1/2]

template<typename int_type >
constexpr fraction< int_type > jau::operator+ ( const fraction< int_type > &  lhs,
const fraction< int_type > &  rhs 
)
constexprnoexcept

Returns sum of two fraction.

Operation may set the overflow flag in the returned instance, if occurring.

Template Parameters
int_typeintegral type
Parameters
lhsa fraction
rhsa fraction
Returns
resulting new fraction, reduced

Definition at line 684 of file fraction_type.hpp.

◆ operator-() [1/2]

template<typename int_type >
constexpr fraction< int_type > jau::operator- ( const fraction< int_type > &  lhs,
const fraction< int_type > &  rhs 
)
constexprnoexcept

Returns difference of two fraction.

Operation may set the overflow flag in the returned instance, if occurring.

Template Parameters
int_typeintegral type
Parameters
lhsa fraction
rhsa fraction
Returns
resulting new fraction, reduced

Definition at line 701 of file fraction_type.hpp.

◆ operator*() [3/3]

template<typename int_type >
constexpr fraction< int_type > jau::operator* ( const fraction< int_type > &  lhs,
const fraction< int_type > &  rhs 
)
constexprnoexcept

Returns product of two fraction.

Operation may set the overflow flag in the returned instance, if occurring.

Template Parameters
int_typeintegral type
Parameters
lhsa fraction
rhsa fraction
Returns
resulting new fraction, reduced

Definition at line 718 of file fraction_type.hpp.

◆ operator/() [3/3]

template<typename int_type >
constexpr fraction< int_type > jau::operator/ ( const fraction< int_type > &  lhs,
const fraction< int_type > &  rhs 
)
constexprnoexcept

Returns division of two fraction.

Template Parameters
int_typeintegral type
Parameters
lhsa fraction
rhsa fraction
Returns
resulting new fraction, reduced

Definition at line 732 of file fraction_type.hpp.

◆ to_fraction_i64()

bool jau::to_fraction_i64 ( fraction_i64 result,
const std::string &  value,
const fraction_i64 min_allowed,
const fraction_i64 max_allowed 
)
noexcept

Stores the fraction_i64 value of the given string value in format <num>/<denom>, which may contain whitespace.

It the given string value does not conform with the format or exceeds the given value range, false is being returned.

If the given string value has been accepted, it is stored in the result reference and true is being returned.

Parameters
resultstorage for result is value is parsed successfully and within range.
valuethe string value
min_allowedthe minimum allowed value
max_allowedthe maximum allowed value
Returns
true if value has been accepted, otherwise false

Definition at line 512 of file basic_types.cpp.

Here is the caller graph for this function:

◆ to_string() [2/2]

std::string jau::to_string ( const fraction_timespec v)
inlinenoexcept

Definition at line 1031 of file fraction_type.hpp.

Here is the caller graph for this function:

◆ operator!=() [2/2]

constexpr bool jau::operator!= ( const fraction_timespec lhs,
const fraction_timespec rhs 
)
constexprnoexcept

Definition at line 1034 of file fraction_type.hpp.

◆ operator==() [2/2]

constexpr bool jau::operator== ( const fraction_timespec lhs,
const fraction_timespec rhs 
)
constexprnoexcept

Definition at line 1038 of file fraction_type.hpp.

◆ operator>() [2/2]

constexpr bool jau::operator> ( const fraction_timespec lhs,
const fraction_timespec rhs 
)
constexprnoexcept

Definition at line 1042 of file fraction_type.hpp.

◆ operator>=() [2/2]

constexpr bool jau::operator>= ( const fraction_timespec lhs,
const fraction_timespec rhs 
)
constexprnoexcept

Definition at line 1046 of file fraction_type.hpp.

◆ operator<() [2/2]

constexpr bool jau::operator< ( const fraction_timespec lhs,
const fraction_timespec rhs 
)
constexprnoexcept

Definition at line 1050 of file fraction_type.hpp.

◆ operator<=() [2/2]

constexpr bool jau::operator<= ( const fraction_timespec lhs,
const fraction_timespec rhs 
)
constexprnoexcept

Definition at line 1054 of file fraction_type.hpp.

◆ max() [2/2]

constexpr const fraction_timespec & jau::max ( const fraction_timespec lhs,
const fraction_timespec rhs 
)
constexprnoexcept

Return the maximum of the two given fraction_timespec.

Definition at line 1059 of file fraction_type.hpp.

Here is the caller graph for this function:

◆ min() [2/2]

constexpr const fraction_timespec & jau::min ( const fraction_timespec lhs,
const fraction_timespec rhs 
)
constexprnoexcept

Return the minimum of the two given fraction_timespec.

Definition at line 1064 of file fraction_type.hpp.

Here is the caller graph for this function:

◆ sign() [2/2]

constexpr snsize_t jau::sign ( const fraction_timespec rhs)
constexprnoexcept

Returns the value of the sign function applied to tv_sec.

-1 for numerator < 0
 0 for numerator = 0
 1 for numerator > 0
Returns
function result

Definition at line 1077 of file fraction_type.hpp.

◆ abs() [2/2]

constexpr fraction_timespec jau::abs ( const fraction_timespec rhs)
constexprnoexcept

Returns the absolute fraction_timespec.

Definition at line 1084 of file fraction_type.hpp.

◆ operator+() [2/2]

constexpr fraction_timespec jau::operator+ ( const fraction_timespec lhs,
const fraction_timespec rhs 
)
constexprnoexcept

Returns sum of two fraction_timespec.

Parameters
lhsa fraction_timespec
rhsa fraction_timespec
Returns
resulting new fraction_timespec, each component reduced and both fraction_timespec::normalize() 'ed

Definition at line 1098 of file fraction_type.hpp.

◆ operator-() [2/2]

constexpr fraction_timespec jau::operator- ( const fraction_timespec lhs,
const fraction_timespec rhs 
)
constexprnoexcept

Returns difference of two fraction_timespec.

See fraction_timespec::to_fraction_i64().

Parameters
lhsa fraction_timespec
rhsa fraction_timespec
Returns
resulting new fraction_timespec, each component reduced and both fraction_timespec::normalize() 'ed

Definition at line 1113 of file fraction_type.hpp.

◆ sleep_until()

void jau::sleep_until ( const fraction_timespec absolute_time,
const bool  monotonic = true 
)
noexcept

sleep_until causes the current thread to block until the specific time is reached.

Method works similar to std::this_thread::sleep_until(), but utilizes fraction_timespec instead of int64_t nanoseconds counter for maintaining high-precision and infinite range.

Implementation also uses ::clock_nanosleep(), with absolute time and either monotonic- or wall-clok time depending on given monotonic flag. This instead of ::nanosleep() with undefined clock-type and interruptions.

Parameters
absolute_timean object of type fraction_timespec representing the time when to stop waiting
monotonicif true, implementation uses the fast and steady monotonic clock (default), otherwise the wall-clock
See also
sleep_until()
sleep_for()
wait_until()
wait_for()

Definition at line 104 of file basic_types.cpp.

Here is the caller graph for this function:

◆ sleep_for() [1/2]

void jau::sleep_for ( const fraction_timespec relative_time,
const bool  monotonic = true 
)
noexcept

sleep_for causes the current thread to block until a specific amount of time has passed.

Implementation calls sleep_until() passing absolute time derived via getMonotonicTime() or getWallClockTime(), see:

  fraction_timespec absolute_time = ( monotonic ? getMonotonicTime() : getWallClockTime() ) + relative_time;

Method works similar to std::this_thread::sleep_until(), but utilizes fraction_timespec instead of int64_t nanoseconds counter for maintaining high-precision and infinite range.

Parameters
relative_timean object of type fraction_timespec representing the the maximum time to spend waiting
monotonicif true, implementation uses the fast and steady monotonic clock (default), otherwise the wall-clock
See also
sleep_until()
sleep_for()
wait_until()
wait_for()

Definition at line 116 of file basic_types.cpp.

Here is the caller graph for this function:

◆ sleep_for() [2/2]

void jau::sleep_for ( const fraction_i64 relative_time,
const bool  monotonic = true 
)
noexcept

sleep_for causes the current thread to block until a specific amount of time has passed.

Implementation calls sleep_until() passing absolute time derived via getMonotonicTime() or getWallClockTime(), see:

  fraction_timespec absolute_time = ( monotonic ? getMonotonicTime() : getWallClockTime() ) + relative_time;

Method works similar to std::this_thread::sleep_until(), but utilizes fraction_timespec instead of int64_t nanoseconds counter for maintaining high-precision and infinite range.

Parameters
relative_timean object of type fraction_i64 representing the the maximum time to spend waiting, which is limited to 292 years if using nanoseconds fractions.
monotonicif true, implementation uses the fast and steady monotonic clock (default), otherwise the wall-clock
See also
sleep_until()
sleep_for()
wait_until()
wait_for()

Definition at line 124 of file basic_types.cpp.

◆ wait_until()

std::cv_status jau::wait_until ( std::condition_variable &  cv,
std::unique_lock< std::mutex > &  lock,
const fraction_timespec absolute_time,
const bool  monotonic = true 
)
noexcept

wait_until causes the current thread to block until the condition variable is notified, a specific time is reached, or a spurious wakeup occurs.

Method works similar to std::condition_variable::wait_until(), but utilizes fraction_timespec instead of int64_t nanoseconds counter for maintaining high-precision and infinite range.

Parameters
cvstd::condition_variable instance
lockan object of type std::unique_lock<std::mutex>, which must be locked by the current thread
absolute_timean object of type fraction_timespec representing the time when to stop waiting
monotonicif true, implementation uses the fast and steady monotonic clock (default), otherwise the wall-clock
Returns
std::cv_status::timeout if the relative timeout specified by rel_time expired, std::cv_status::no_timeout otherwise.
See also
sleep_until()
sleep_for()
wait_until()
wait_for()

Definition at line 178 of file basic_types.cpp.

Here is the caller graph for this function:

◆ wait_for() [1/2]

std::cv_status jau::wait_for ( std::condition_variable &  cv,
std::unique_lock< std::mutex > &  lock,
const fraction_timespec relative_time,
const bool  monotonic = true 
)
noexcept

wait_for causes the current thread to block until the condition variable is notified, a specific amount of time has passed, or a spurious wakeup occurs.

Implementation calls wait_until() passing absolute time derived via getMonotonicTime() or getWallClockTime(), see:

  fraction_timespec absolute_time = ( monotonic ? getMonotonicTime() : getWallClockTime() ) + relative_time;

Method works similar to std::condition_variable::wait_for(), but utilizes fraction_timespec instead of int64_t nanoseconds counter for maintaining high-precision and infinite range.

When using a condition predicate loop to ensure no spurious wake-up preemptively ends waiting for the condition variable event or timeout, it is always recommended to use wait_until() using the absolute timeout time computed once before said loop. Example from latch::wait_for():

     std::unique_lock<std::mutex> lock(mtx_cd);
     const fraction_timespec timeout_time = getMonotonicTime() + timeout_duration;
     while( 0 < count ) {
         std::cv_status s = wait_until(cv, lock, timeout_time);
         if( 0 == count ) {
             return true;
         }
         if( std::cv_status::timeout == s ) {
             return false;
         }
     }
Parameters
cvstd::condition_variable instance
lockan object of type std::unique_lock<std::mutex>, which must be locked by the current thread
relative_timean object of type fraction_timespec representing the the maximum time to spend waiting
monotonicif true, implementation uses the fast and steady monotonic clock (default), otherwise the wall-clock
Returns
std::cv_status::timeout if the relative timeout specified by rel_time expired, std::cv_status::no_timeout otherwise.
See also
sleep_until()
sleep_for()
wait_until()
wait_for()

Definition at line 196 of file basic_types.cpp.

◆ wait_for() [2/2]

std::cv_status jau::wait_for ( std::condition_variable &  cv,
std::unique_lock< std::mutex > &  lock,
const fraction_i64 relative_time,
const bool  monotonic = true 
)
noexcept

wait_for causes the current thread to block until the condition variable is notified, a specific amount of time has passed, or a spurious wakeup occurs.

Implementation calls wait_until() passing absolute time derived via getMonotonicTime() or getWallClockTime(), see:

  fraction_timespec absolute_time = ( monotonic ? getMonotonicTime() : getWallClockTime() ) + relative_time;

Method works similar to std::condition_variable::wait_for(), but utilizes fraction_timespec instead of int64_t nanoseconds counter for maintaining high-precision and infinite range.

When using a condition predicate loop to ensure no spurious wake-up preemptively ends waiting for the condition variable event or timeout, it is always recommended to use wait_until() using the absolute timeout time computed once before said loop. Example from latch::wait_for():

     std::unique_lock<std::mutex> lock(mtx_cd);
     const fraction_timespec timeout_time = getMonotonicTime() + timeout_duration;
     while( 0 < count ) {
         std::cv_status s = wait_until(cv, lock, timeout_time);
         if( 0 == count ) {
             return true;
         }
         if( std::cv_status::timeout == s ) {
             return false;
         }
     }
Parameters
cvstd::condition_variable instance
lockan object of type std::unique_lock<std::mutex>, which must be locked by the current thread
relative_timean object of type fraction_i64 representing the the maximum time to spend waiting, which is limited to 292 years if using nanoseconds fractions.
monotonicif true, implementation uses the fast and steady monotonic clock (default), otherwise the wall-clock
Returns
std::cv_status::timeout if the relative timeout specified by rel_time expired, std::cv_status::no_timeout otherwise.
See also
sleep_until()
sleep_for()
wait_until()
wait_for()

Definition at line 204 of file basic_types.cpp.