【发布时间】:2014-06-11 15:30:12
【问题描述】:
CLOCK_REALTIME 的一个问题是它不是单调的,如果发生 NTP 同步,时间可能会倒退。
执行以下操作使其单调是否安全?
struct timespec GetMonotonicTime()
{
static struct timespec last = timespec();
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
last.tv_nsec = (last.tv_sec == ts.tv_sec) ? std::max(last.tv_nsec, ts.tv_nsec) : ts.tv_nsec;
last.tv_sec = std::max(last.tv_sec, ts.tv_sec);
return last;
}
【问题讨论】: