比较两个timespec值的最佳方式是什么,以查看哪个先发生?
下面的内容有什么问题吗?
bool BThenA(timespec a, timespec b) {
//Returns true if b happened first -- b will be "lower".
if (a.tv_sec == b.tv_sec)
return a.tv_nsec > b.tv_nsec;
else
return a.tv_sec > b.tv_sec;
}发布于 2015-06-17 23:51:23
另一种方法是为timespec定义一个全局operator <()。然后你可以只比较一下,如果一次发生在另一次之前。
bool operator <(const timespec& lhs, const timespec& rhs)
{
if (lhs.tv_sec == rhs.tv_sec)
return lhs.tv_nsec < rhs.tv_nsec;
else
return lhs.tv_sec < rhs.tv_sec;
}然后在你的代码中,你可以拥有
timespec start, end;
//get start and end populated
if (start < end)
cout << "start is smaller";https://stackoverflow.com/questions/30895970
复制相似问题