$include_dir="/home/hyper-archives/boost/include"; include("$include_dir/msg-header.inc") ?>
From: Hillel Y. Sims (hsims_at_[hidden])
Date: 2002-10-17 16:15:45
"David Abrahams" <dave_at_[hidden]> wrote in message
news:ur8epu5y8.fsf_at_boost-consulting.com...
> Jonathan Biggar <jon_at_[hidden]> writes:
>
[ "canonical form" ]
> > > >     lock->acquire();
> > > >     while (!predicate())
> > > >       cond.wait();
> > > >     // modify protected data here
> > > >     lock->release();
> > >
>
>      template <class ScopedLock, class Predicate>
>      wait(ScopedLock& lock, Predicate pred)
>      {
>          if (!lock.locked())
>              lock.lock(); // instead of throw
>          while (!pred())
>              wait(lock);
>      }
>
It might be slightly confusing that one version of wait() (the non-predicate
form) will require the lock to be locked (as pointed out already, otherwise
you just can't safely check the predicate, which is a prerequisite to
calling wait()), while the predicate-based form will not. (Anyhow, how do
you have a reference to a ScopedLock object without already holding the
lock, except by malicious intent?)
(I like the notion of predicate-based wait() wrapper though, might help to
eliminate some confusion over whether/why one should say "while (!pred)
cv.wait()" vs. "do { cv.wait() } while (!pred)".)
hys
-- Hillel Y. Sims FactSet Research Systems hsims AT factset.com