2010-02-13 50 views
1

我想等待條件達1秒鐘。我已經嘗試傳遞TIME_DURATION:如何獲得boost :: condition :: timed_wait來編譯?

boost::posix_time::time_duration td = boost::posix_time::milliseconds(50); 
readerThread_cond_.timed_wait(lock, boost::bind(&XXXX::writeCondIsMet, this), td); 

,但我得到的錯誤:

/usr/include/boost/thread/pthread/condition_variable.hpp:156: error: no match for ‘operator+’ in ‘boost::get_system_time() + wait_duration’

我也試過路過一個xtime的:

boost::xtime t; 
boost::xtime_get(&t, boost::TIME_UTC); 
readerThread_cond_.timed_wait(lock, boost::bind(&XXXX::writeCondIsMet, this), td); 

,但我得到的錯誤:

我與libboost_thread和libboost_date_time鏈接,代碼編譯並運行正常,當我使用只是使用等待,但錯誤信息似乎與解決boost頭文件中的模板有關。這似乎是說我沒有通過正確的事情,但我只是不明白這一點。

回答

1

我認爲這是參數順序。

由於我從來沒有遇到過與timed_wait有關的問題,所以我在the boost reference to boost.thread, condition_variable_any, timed_wait上查看了一些細節。我覺得最有趣的是這樣的:

template<typename lock_type,typename duration_type,typename predicate_type> 
bool timed_wait(lock_type& lock,duration_type const& rel_time,predicate_type predicate); 

持續時間實際上是第二個參數,而不是第三個參數。

[編輯]順便說一句,你真的應該檢查timed_wait的返回值,否則你不會知道的條件是否你有信號,或出現超時。 timed_wait不是由於超時拋出![/編輯]

+0

我其實在一分鐘前計算出來,但你得到的功勞。謝謝 – 2010-02-13 21:45:04