Does someone have an example of how to most easily use boost::condition::timed_wait? There are some threads on the topic here, here and here, but none feature a working example. And boost doc is as usual quite sparse.
Usage example of boost::condition::timed_wait
Asked Answered
Actually, I finally found a link with full example here. With a bit of adapting, this seems to be the call.
boost::system_time const timeout=boost::get_system_time()+ boost::posix_time::milliseconds(35000);
boost::mutex::scoped_lock lock(the_mutex);
if(the_condition_variable.timed_wait(lock,timeout,&CondFulfilled))
{
<cond fulfilled code>
}
else
{
<timeout code>
}
bool CondFulfilled() { ... }
© 2022 - 2024 — McMap. All rights reserved.
while(!data_ready) { cond.wait(lock); }
. For timed, it only says The duration overload of timed_wait is difficult to use correctly. The overload taking a predicate should be preferred in most cases. I don't understand that though, nor this snippet:while(!pred()) { if(!timed_wait(lock,abs_time)) { return pred(); } } return true;
What is pred() supposed to be? – Otti