使用时timed_wait
on a boost::condition_variable
如果有持续时间,即使用户(或 ntp)更改系统时间,等待条件也会在持续时间后超时吗?
E.g.,
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 月),如果挂钟时间在您等待升压条件变量时发生变化,您只会得到不好的结果。
If you don't必须使用boost,你可以使用所谓的“单调时钟”。由于单调时钟不受挂钟时间变化的影响,因此它不易受到您所描述的问题的影响。您可以使用 pthreads API 安全地等待 5 秒,如下所示:
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 http://svn.boost.org/svn/boost/trunk/boost/thread/pthread/condition_variable.hpp
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)