【发布时间】:2020-01-17 13:58:38
【问题描述】:
以下sn-p的代码:
struct timespec ts;
for (int x = 0; x < 100000000; x++) {
timespec_get(&ts, TIME_UTC);
long cTime = (long) time(NULL);
if (cTime != ts.tv_sec && ts.tv_nsec < 3000000) {
printf("cTime: %ld\n", cTime);
printf("ts.tv_sec: %ld\n", ts.tv_sec);
printf("ts.tv_nsec: %ld\n", ts.tv_nsec);
}
}
产生这个输出:
...
cTime: 1579268059
ts.tv_sec: 1579268060
ts.tv_nsec: 2527419
cTime: 1579268059
ts.tv_sec: 1579268060
ts.tv_nsec: 2534036
cTime: 1579268059
ts.tv_sec: 1579268060
ts.tv_nsec: 2540359
cTime: 1579268059
ts.tv_sec: 1579268060
ts.tv_nsec: 2547039
...
为什么cTime 和ts.tv_sec 之间存在差异?请注意,如果条件更改为ts.tv_nsec >= 3000000,则不会出现此问题。该问题依赖于小于 3000000 的纳秒。
【问题讨论】:
-
您需要更具体地了解所使用的操作系统、操作系统版本、使用的 C 库版本。
-
@Someprogrammerdude Linux Debian 8,GCC 6.3.0。
-
timespec_get()是什么?这是 C 还是 C++?看起来像std::timespec_get。请使用适当的标签。 -
@MarcoBonelli:它是在 C11 中添加到 C 中的。 Can reproduce online.
-
@ShadowRanger 谢谢你的参考,我在我的系统上看不到
timespec_get的man条目,所以我匆忙下结论。有道理。