views:

212

answers:

1

Hi... I'm wondering what I'm doing wrong... with a sole wait it compiles and runs, but not with a timed_wait:

using boost::interprocess::scoped_lock;
using boost::interprocess::interprocess_mutex;
using boost::posix_time::milliseconds;

[...]

scoped_lock<interprocess_mutex> lock(obj->mutex);
while (...) {
   obj->condition.timed_wait(lock, milliseconds(100));
}

where obj->mutex is a boost::interprocess::interprocess_mutex and obj->condition is a boost::interprocess::interprocess_condition. Here is the g++ error log:

code.cpp: In member function ‘[...]’:
code.cpp:42: error: no matching function for call to ‘boost::interprocess::interprocess_condition::timed_wait(boost::interprocess::scoped_lock<boost::interprocess::interprocess_mutex>&, boost::posix_time::milliseconds)

whereas this is the prototype of the condition class member function (boost/interprocess/sync/interprocess_condition.hpp):

template <typename L, typename Pr>
   bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time, Pr pred)

(boost 1.40)

+2  A: 

Thanks to a nice #boost IRC user (mjcaisse), I have now a clue: timed_wait needs an absolute time.

bool noTimeout = true;
boost::system_time timeout = boost::get_system_time() + milliseconds(10);
while (!noTimeout && [CONDITION NOT MET])
{
   noTimeout = obj->condition.timed_wait(lock, timeout);
}

if (!noTimeout)
{
   std::cout << "timeout!" << std::endl;
}
moala