condition_variable::timed_wait doesn't wait

Hi! I'm trying to use condition_variable::timed_wait without success. Using the following code, the timed_wait returns immediatly also if Receive() is never called. Where I wrong? void Receive() { boost::lock_guardboost::mutex lock(_mut); //... code ...// _data_ready = true; // Notify and unlock the mutex _cond.notify_one(); } bool Send() { boost::unique_lockboost::mutex lock(_mut); boost::xtime xt; xtime_get(&xt, boost::TIME_UTC); xt.sec += 5; _data_ready = false; //... send data ...// //wait for an answer.. bool ready = _cond.timed_wait(lock, xt, boost::lambda::var(_data_ready)); return ready; } I've also tried to use: ptime t = second_clock::universal_time(); ptime timeout = t + milliseconds(cmd_timeout); bool ready = _cond.timed_wait(lock, timeout, boost::lambda::var(_data_ready)); but with the same effect. Thanks, Daniele.

Daniele Barzotti
Hi! I'm trying to use condition_variable::timed_wait without success. Using the following code, the timed_wait returns immediatly also if Receive() is never called. [snip] Where I wrong?
I haven't used boost's locking mechanisms yet, but POSIX condvars don't protect against spurious wakes. Thus, [snip]
//wait for an answer.. bool ready = _cond.timed_wait(lock, xt, boost::lambda::var(_data_ready));
you probably want this in a loop, while(!_data_ready) { _cond.timed_wait(... _data_ready); } HTH, -tom

2009/3/9 tom fogal
I haven't used boost's locking mechanisms yet, but POSIX condvars don't protect against spurious wakes. Thus,
[snip]
//wait for an answer.. bool ready = _cond.timed_wait(lock, xt, boost::lambda::var(_data_ready));
you probably want this in a loop,
while(!_data_ready) { _cond.timed_wait(... _data_ready); }
timed_wait with predicate (that's what OP used) does exactly this, it waits on condvar until predicate is satisfied. So problem is in some other place. Roman.
participants (3)
-
Daniele Barzotti
-
Roman Perepelitsa
-
tom fogal