如果未设置clock_settime,clock_gettime() 是否有效?
Does clock_gettime() work if clock_settime has not been set?
如果时钟没有被clock_settime()设置,clock_gettime()是否有效?我遇到重复调用 clock_gettime() 返回相同时间值(秒)的情况。我似乎记得我需要先用 clock_settime() 设置时间。clock_gettime() 和 time() 是否使用相同的时间源?
clock_gettime(clockid_t clk_id, struct timespec *tp) - All implementations support the system-wide real-time clock, which is identified by CLOCK_REALTIME. Its time represents seconds and nanoseconds SINCE THE EPOCH. When its time is changed, timers for a relative interval are unaffected, but timers for an absolute point in time are affected.
time() - returns the time as the number of seconds SINCE THE EPOCH, 1970-01-01 00:00:00 +0000 (UTC).
代码是C,平台是Ubuntu 18.04。C++在这个问题中被标记,因为据我所知,C++没有提供获得高精度时间的方法(尽管 C++20 提供了),我将创建我的代码的C++版本。
clock_settime
设置整个计算机的时间。 只有系统管理工具(例如GNOME控制中心)和时间同步守护进程(例如ntpd)才需要调用它。 它需要超级用户权限;大多数程序无法成功调用它。
如果从未在特定计算机上调用过clock_settime
(或执行相同工作的旧 API 之一),则与当前时间的某些权威源相比,clock_gettime
返回的值很可能是错误的。 但是,无论时间是否准确,时钟都应该仍在运行。 我不知道在 Linux 或其他设备上有任何 API 可以停止系统时钟。
但是,根据您使用的时钟 ID,从对clock_gettime
的两次连续调用中获得相同的值可能是正常的(不是错误或问题)。 例如,如果两个调用之间的间隔短于时钟的分辨率,则可能会发生这种情况,如clock_getres
报告的那样。
相关文章:
- 在提升multi_index容器中,是否定义了"default index"?
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 检查输入是否不是整数或数字
- 是否可以初始化不可复制类型的成员变量(或基类)
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- 是否可以通过C++扩展强制多个python进程共享同一内存
- 此代码是否违反一个定义规则
- 是否需要删除包含对象的"pair"?
- 是否可以从int转换为enum类类型
- 无论条件是否为true,if总是在c++中执行
- 如何找到大小'x'数组是否完全填充,在C++?
- 检查值是否在集合p1和p2中,但不在p3中
- 是否可以在编译时初始化数组,以便在运行时不会花费时间?
- 检查 std::shared_ptr<> 的当前底层类型是否为 T
- 在c++中检查长方体是否尽可能快地重叠(无迭代)
- GL_SHADERSTORAGE_BUFFER位置是否与其他着色器位置冲突
- 子目录是否继承属性,例如add_definitions,include_directories和父Cmakelist.t
- 标准是否使用多余的大括号(例如 T{{{10}}})定义列表初始化?
- C/C++预处理器是否可以检测一些编译器选项
- c++ 中的 clock() 是否与繁重的 CPU 负载一致