Boost logo

Boost :

From: Greg Colvin (gcolvin_at_[hidden])
Date: 2000-08-18 14:33:23


From: "William Kempf" <sirwillard_at_[hidden]>
> --- In boost_at_[hidden], "Greg Colvin" <gcolvin_at_u...> wrote:
> > From: "William Kempf" <sirwillard_at_m...>
> > > ...
> > > The problem with the CV is that we've only insured that the
> > > associated mutex is locked in the call to wait() but not in the call
> > > to notify(). It would be nice if we could have some manner in which
> > > we can insure that we've locked the mutex at compile time for both of
> > > these, though I think we'll likely have to settle for an exception at
> > > run time. If you've seen Jeremy's documentation maybe you have some
> > > thoughts on this?
> >
> > Earlier I suggested that the lock, wait condition, and notify could be
> > combined into a single object with a templated constructor.
>
> This doesn't address the problem. Just combining them into a single
> object won't solve the problem of needing the mutex to be locked
> during calls to wait() and notify().
>
> Besides, the lock can't be combined in this way ...

Maybe I'm missing something, but I don't see why something like
following wouldn't work:

   struct mutex {
      struct condition {
         condition(mutex*);
         bool wait(int);
         void notify();
      };
      struct timeout_exception : exception {};
      struct wait {
         wait(mutex*, lambda_functor predicate,
              condition in,condition out,int timeout=-1)
         : mtx(mtx), in(in), out(out) {
            mtx->enter();
            do {
               if (in.wait(timeout))
                  throw(timeout_exception());
            } while(!predicate());
         }
         ~wait() {
            out.notify();
            mtx->exit();
         }
         mutex* mtx;
         condition in;
         condition out;
      };
      void enter();
      void exit();
   };


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