ind*_*div 8 c++ boost timeout condition-variable time-wait
当使用timed_wait在boost::condition_variable具有持续时间,将等待状态超时即使用户(或NTP)更改系统时间的持续时间后?
例如,
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?
}
Run Code Online (Sandbox Code Playgroud)
截至撰写之日(2013年11月),如果在等待提升条件变量时挂钟时间发生变化,您只会得到不好的结果.
如果您不必使用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);
Run Code Online (Sandbox Code Playgroud)
您可以检查boost :: condition_variable的实现.也许他们有一天会解决这个问题.实现在这里:http://svn.boost.org/svn/boost/trunk/boost/thread/pthread/condition_variable.hpp