std::clock(), threading, and Linux
I had a strange experience with the boost::timer class the other day, since it never elapsed within a certain thread. As it turns out, this was because std::clock() itself doesn’t tick but returns the same value over and over again - a value that probably indicates the time the tread was started. The solution is rather simple: I implemented the boost::timer interface based upon boost::xtime from the boost threading library like this:
class XTimer {
public:
XTimer() {
xtime_get(&_start_time, boost::TIME_UTC);
}
XTimer(const XTimer& other)
: _start_time(other._start_time)
{
}
~XTimer() {
}
double elapsed() const {
boost::xtime now;
xtime_get(&now, boost::TIME_UTC);
return boost::lexical_cast<double>(now.sec - _start_time.sec);
}
private:
boost::xtime _start_time;
};