Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2001-08-16 13:22:16


From: "Greg Colvin" <gcolvin_at_[hidden]>
> From: "Peter Dimov" <pdimov_at_[hidden]>
> > From: <williamkempf_at_[hidden]>
> > > Well, the preferred method shortens the pattern a little:
> > >
> > > {
> > > mutex::scoped_lock lock(m);
> > > cv.wait(lock, pred);
> > > action();
> > > }
> >
> > Yes; the real problem is that without Lambda, there is no simple way to
> > define 'pred' in-place.
> >
> > The perfect syntax would be
> >
> > cv.wait(q.empty(), command = q.pop());

Of course I forgot the mutex parameter. :-)

> > How close can we get within std C++ to it?
>
> I should think that with expression templates you could get
> very close.

Hmm. This line of thought leads to

class condition
{
  template<class Mutex, class Pred, class Action> typename
Action::result_type wait(Mutex & m, Pred pr, Action a)
  {
    typename Mutex::scoped_lock lock(m);
    this->wait(lock, pred);
    return a();
  }
};

and now the example is

int command = cv.wait(m, bind(&Queue::empty, &q), bind(&Queue::pop, &q));

Not bad. Thanks for the inspiration. You know, that 'bind' thing is cool.
:-))

--
Peter Dimov
Multi Media Ltd.

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