c++ - 稳定的时钟在系统范围内稳定吗?

标签 c++ c++11 time

我正在使用 FastRTPS 在单个 Linux 系统上的多个进程之间进行通信。它们以数据包的形式交换信息。每个数据包都有一个与其发送或接收时间无关的时间戳。这样可以正确使用传达的信息。

我在考虑使用:

uint64_t time_in_microseconds = std::chrono::duration_cast
          <std::chrono::microseconds>(std::chrono::steady_clock::now()
          .time_since_epoch()).count(); 

获取数据包的时间戳。

但是,在单个系统上跨进程的稳定时钟是否稳定?还是只在一个进程内?

如果不是,系统时钟在正常情况下会有多少变化?它会“回到过去”多少? (没有手动调整,没有网络连接,没有时间更改等)

谢谢

编辑:

数据包将用于状态估计和控制算法。例如,传感器数据将从传感器读取过程转移到状态估计过程。状态信息将从估计过程转移到控制过程。这就是为什么我需要能够在整个系统中一致地测量间隔。 system_clock 和 steady_clock 似乎都不能提供我需要的东西。 System_clock 是一致的,但不是单调的。稳定的时钟在单个进程内是单调且一致的,但据我所知,它在整个系统中并不一致?或者是吗?

最佳答案

steady_clock更适合测量区间。

std::chrono::steady_clock represents a monotonic clock. The time points of this clock cannot decrease as physical time moves forward and the time between ticks of this clock is constant. This clock is not related to wall clock time (for example, it can be time since last reboot), and is most suitable for measuring intervals.

system_clock是系统范围的,可能是你需要的,但可能不是单调的:

Class std::chrono::system_clock represents the system-wide real time wall clock. It may not be monotonic: on most systems, the system time can be adjusted at any moment. It is the only C++ clock that has the ability to map its time points to C-style time, and, therefore, to be displayed [until C++20]

纪元将从 c++20

更改为

system_clock measures Unix Time (i.e., time since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970, not counting leap seconds). [since c++20]

关于c++ - 稳定的时钟在系统范围内稳定吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56690910/

相关文章:

c++ - 在 proxygen 中获取 header 值

c++ - 如何在 std::vector 中找到一个对象

c++ - C++ 中 std::resize(n) 和 std::shrink_to_fit 的区别?

python - 在数据框中使用 For 循环将字符串转换为时间

javascript - 为什么 iPhone iOS 显示 momentjs 的无效日期

c++ - 我怎样才能使 Eclipse C.D.T.认识 C++11 的特性?

c++ - 为什么malloc总是分配内存失败(大约9GB,但我有16GB物理内存)?

c++ - 在将实例作为基础对象传递的函数中使用复制构造函数创建实例的拷贝

c++ - 返回常量对象并将其分配给非常量对象

c++ - 将秒数转换为时间