我如何获得提振::状态:: TIMED_WAIT编译? [英] How do I get boost::condition::timed_wait to compile?
问题描述
我想在条件最多等待1秒。我已经尝试传递TIME_DURATION:
I want to wait on a condition for up to 1 second. I've try passing in time_duration:
boost::posix_time::time_duration td = boost::posix_time::milliseconds(50);
readerThread_cond_.timed_wait(lock, boost::bind(&XXXX::writeCondIsMet, this), td);
但我得到的错误:
but I get the error:
/usr/include/boost/thread/pthread/condition_variable.hpp:156:
错误:没有对应于运营商+'
提振:: get_system_time()+
wait_duration
/usr/include/boost/thread/pthread/condition_variable.hpp:156: error: no match for ‘operator+’ in ‘boost::get_system_time() + wait_duration’
我也试过路过一个xtime的:
I've also tried passing a xtime:
boost::xtime t;
boost::xtime_get(&t, boost::TIME_UTC);
readerThread_cond_.timed_wait(lock, boost::bind(&XXXX::writeCondIsMet, this), td);
但我得到的错误:
but I get the error:
我与libboost_thread和libboost_date_time和code编译链接和运行正常,当我用只是使用的等待,但似乎错误消息有关解决升压头文件的模板。这似乎是说我没有传递正确的事情,但我就是不明白。
I'm linking with libboost_thread and libboost_date_time and the code compiles and run ok when I use just use wait, but the error message it seems to related to resolving the templates in the boost header files. It seems to be saying I am not passing in the right thing, but I just don't understand it.
推荐答案
我觉得它的参数顺序。
由于我从来没有与 TIMED_WAIT
一个问题,我看了一些细节处<一个href=\"http://www.boost.org/doc/libs/1_37_0/doc/html/thread/synchronization.html#thread.synchronization.condvar_ref.condition_variable.timed_wait_$p$pdicate\"相对=nofollow>升压参考boost.thread,condition_variable_any,TIMED_WAIT 。我觉得最有趣的是这样的:
As I've never had a problem with timed_wait
, I looked at some details at the boost reference to boost.thread, condition_variable_any, timed_wait. What I find most interesting is this:
template<typename lock_type,typename duration_type,typename predicate_type>
bool timed_wait(lock_type& lock,duration_type const& rel_time,predicate_type predicate);
时间持续时间实际上是第二个参数,而不是第三。
The time-duration is actually the second argument, not the third.
顺便说一句,你真的应该检查 TIMED_WAIT
的返回值,否则你不会知道的条件是否你有信号的,或发生超时。 TIMED_WAIT
将不可以抛由于超时![/编辑]
[edit] BTW, you really should check the return value of timed_wait
, as otherwise you won't know whether you the condition got signaled, or a timeout occurred. timed_wait
will not throw due to a timeout![/edit]
这篇关于我如何获得提振::状态:: TIMED_WAIT编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!