Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2002-10-17 16:21:03


"Hillel Y. Sims" <hsims_at_[hidden]> writes:

> "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.

There's still the unanswered question of why we have the
non-predicated form at all, adorned as it is by warnings of "danger!"

> (Anyhow, how do you have a reference
> to a ScopedLock object without already holding the lock, except by
> malicious intent?)

By explicitly unlocking it, I guess.

> (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)".)

<confused> We already have a predicate-based wait wrapper, don't we?

-- 
                    David Abrahams
dave_at_[hidden] * http://www.boost-consulting.com
Building C/C++ Extensions for Python: Dec 9-11, Austin, TX
http://www.enthought.com/training/building_extensions.html

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk