gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:41:33 24 4
gpt4 key购买 nike

我正在使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com