Get reliable monotonous time in C ++ (multi-platform)

I need a monotonous clock that can be used to calculate intervals. Requirements:

  • It should be monotonous, it should not be affected by the time of the device.
  • It is not necessary to reset during an application session. (The same era for all return values ​​in a session)
  • It should represent real-time seconds (not processor seconds), should not depend on the number of threads / processes running at that time.
  • Allowed seconds resolution.

In my research, I found Candidates:

  • std::clock()(ctime) - It seems you need to use cpu seconds
  • boost::chrono::steady_clock()- Does he use cpu seconds? Can the era change during the application session(launch-end)?
  • Platform-specific methods ( clock_gettime, mach_absolute_time).

Have you ever encountered such a problem and what solution did you choose? Is it a steady_clock()reliable multi-platform?

+4
source share
2 answers

I would use std :: chrono :: stable_clock . According to the description, it is not affected by changes in the wall clock / system time and is best suited for measuring intervals.

+3
source

boost::chrono::steady_clock()- Does he use cpu seconds? Can an era change during an application session (launch)?

std::chrono::steady_clock() . , . std::chrono::steady_clock, , .

0

Source: https://habr.com/ru/post/1529221/


All Articles