Boost logo

Boost :

From: William Kempf (sirwillard_at_[hidden])
Date: 2000-08-11 17:43:23


--- In boost_at_[hidden], jsiek_at_l... wrote:
> William Kempf writes:
> > mutex mx;
> > condition cv(mx);
> > mutex::lock lock(mx);
> >
> > while (!full) // Just to not cloud things with the predicate
> > lock.wait(cv);
> >
> > You obviously can't wait on the cv unless the mutex is locked.
Since
> > both the lock and the CV were constructed with the mutex it's
> > possible to throw an exception if the two aren't associated with
the
> > same mutex (not as good as a compile time error, but better than
> > nothing).
>
> Or how about this:
>
>
> mutex mx;
> condition cv; // no mutex here
>
> mutex::lock lock(mx);
>
> while (!full)
> lock.wait(cv); // mutex comes from the lock
>
>
> Now it is impossible for the user to use the wrong mutex with the
> codition variable.

I'm not sure I follow. How is the following prohibited?

mutex mx1;
mutex mx2;
condition cv;

mutex::lock lock1(mx1);
mutex::lock lock2(mx2);

while (!full)
{
   lock1.wait(cv);
   lock2.wait(cv);
}


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