I am using timed_wait from the C ++ boost library and I am having a problem with stopwatch.
Here is a quick test:
#include <boost/thread.hpp> #include <stdio.h> #include <boost/date_time/posix_time/posix_time.hpp> int main(){ // Determine the absolute time for this timer. boost::system_time tAbsoluteTime = boost::get_system_time() + boost::posix_time::milliseconds(35000); bool done; boost::mutex m; boost::condition_variable cond; boost::unique_lock<boost::mutex> lk(m); while(!done) { if(!cond.timed_wait(lk,tAbsoluteTime)) { done = true; std::cout << "timed out"; } } return 1; }
The timed_wait function returns 24 seconds earlier than necessary. 24 seconds is the current number of seconds of jump in UTC.
So, boost is widely used, but I could not find any information about this particular problem. Has anyone else experienced this issue? What are the possible causes and solutions?
Notes: I am using boost 1.38 on a Linux system. I heard that this problem does not occur on macOS.
UPDATE: a little more information: this happens on 2 redhat machines with a 2.6.9 kernel. I ran the same code on an ubuntu machine with a 2.6.30 kernel, and the timer behaves as expected.
So, I think this is probably caused by the OS or some misconfiguration on redhat machines.
I encoded a workaround that sets the time to UTC and then gets the difference from this setting and adds to the original time. This seems like a bad idea to me, because if this code is executed on a machine without this problem, it could be 24 Ahad. Still unable to find the reason for this.
source share