比较两个 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;
}
另一种方法是拥有一个全球性的operator <()
定义为timespec
。然后你就可以比较一个事件是否发生在另一个事件之前。
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";
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)