我是通過我的STL實現(標準問題g++ 4.6.2
)閱讀和跨越該位的競爭條件傳來condition_variable
內:實施condition_variable TIMED_WAIT正確
template<typename _Rep, typename _Period>
cv_status
wait_for(unique_lock<mutex>& __lock,
const chrono::duration<_Rep, _Period>& __rtime)
{
return wait_until(__lock, __clock_t::now() + __rtime);
}
因爲__clock_t
是std::chrono::system_clock
,我們綁像NTP這樣的事情(如果時鐘在__clock_t::now() + __rtime
後移動了一天,那麼我們將等待一天)。
C++標準(30.5.1)似乎得到它的權利:
效果:彷彿
return wait_until(lock, chrono::steady_clock::now() + rel_time);
Boost的condition_variable
實現了相同的問題:
template<typename duration_type>
bool timed_wait(unique_lock<mutex>& m,duration_type const& wait_duration)
{
return timed_wait(m,get_system_time()+wait_duration);
}
事實上,底層的並行線程執行,似乎是這個問題:
int pthread_cond_timedwait(pthread_cond_t *restrict cond,
pthread_mutex_t *restrict mutex,
const struct timespec *restrict abstime);
因爲abstime
被指定爲「系統時間」,而不是單調的時鐘。
所以我的問題是:如何正確實施像std::condition_variable::wait_for
這樣的東西?有沒有一個現有的實現來解決這個問題?或者我錯過了什麼?
注意:在內部,'pthread_cond_timedwait'使用'gettimeofday',如果你想在你指定的時間內超時,它就是假的:http://sourceware.org/git/?p=glibc.git;a=blob; f = nptl/pthread_cond_timedwait.c; h = 7278ec45b0eb0b48be50fe832fcd17ae988dca27; hb = HEAD – 2012-08-10 00:50:40
您可能不得不使用一些其他使用單調時鐘的計時器線程,然後在服務員取消之前將服務器喚醒。 – jxh 2012-08-10 00:53:09
如果我必須走得那麼遠(我非常希望我不這樣做),我寧願在我的'wait_for'函數中做一個旋轉等待(看起來像我的'sleep_for'沒有這個問題,因爲'nanosleep'沒有同樣的問題,因爲它正確使用'CLOCK_MONOTONIC')。 – 2012-08-10 00:58:13