问题描述
在持续时间为 boost :: condition_variable
的持续时间上使用 timed_wait
时,即使用户(或ntp)更改了系统,等待条件也会在持续时间后超时时间?
When using timed_wait
on a boost::condition_variable
with a duration, will the wait condition time out after the duration even if the user (or ntp) changes the system time?
例如,
boost::posix_time::time_duration wait_duration(0, 0, 1, 0); // 1 sec
// ** System time jumps back 15 minutes here. **
if( !signal.timed_wait(lock, wait_duration) )
{
// Does this condition happen 1 second later, or about 15 minutes later?
}
推荐答案
截至撰写之日(2013年11月),如果在等待升压条件变量时挂钟时间发生了变化,您只会得到结果不好.
As of the date of writing (Nov 2013), if the wall-clock time changes while you're waiting on a boost condition variable, you simply will get bad results.
如果您不必使用加速功能,则可以使用所谓的单调时钟".由于单调时钟不受挂钟时间更改的影响,因此它不会受到您描述的问题的影响.您可以使用pthreads API安全地等待5秒钟,如下所示:
If you don't have to use boost, you can use what is called the "monotonic clock." Since the monotonic clock is unaffected by wall-clock time changes, it is not vulnerable to the problem you described. You can safely wait 5 seconds using the pthreads API using something like this:
pthread_condattr_t attr;
pthread_cond_t cond;
struct timespec ts;
pthread_condattr_init(&attr);
pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
pthread_cond_init(&cond, &attr);
pthread_condattr_destroy(&attr);
clock_gettime(CLOCK_MONOTONIC, &ts);
ts.tv_sec += 5;
pthreead_cond_timedwait(&cond, &mutex, &ts);
您可以检查boost :: condition_variable的实现.也许他们有一天会解决这个问题.实现在这里: http://svn.boost.org/svn/boost/trunk/boost/thread/pthread/condition_variable.hpp
You can check the implementation of boost::condition_variable. Maybe they will fix this some day. The implementation is here: http://svn.boost.org/svn/boost/trunk/boost/thread/pthread/condition_variable.hpp
这篇关于如果我在进行timed_wait持续时间时系统时间发生了变化,该怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!