简体   繁体   中英

How do I get boost::condition::timed_wait to compile?

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: error: no match for 'operator+' in 'boost::get_system_time() + wait_duration'

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:

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.

I think it's the argument order.

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.

[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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM