Boost logo

Boost :

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


--- In boost_at_[hidden], jsiek_at_l... wrote:
> William Kempf writes:
> > Your code did give me an idea, however. I've been trying to
figure
> > out how you could tie the CV to the mutex and also require the
mutex
> > to be locked before calling wait. Well, you could pass the
mutex in
> > to the CVs constructor for the first part, and place the wait()
> > method in the lock, passing in the CV, for the second part.
We'll
> > still have some pretty tight coupling in my mind with the lock
> > needing to be a friend of the mutex and the CV a friend of the
lock,
> > but it seems "solid" to me at this point.
>
> I have trouble understanding english (even though that's my native
> lang). I understand C++ much better :) Code examples please!

Maybe it's me that has trouble writing english even though it's my
native language ;).

Well, I'm still struggling a little on the tight coupling issues, so
I don't have a full implementation of the idea yet. However, I can
give example code to illustrate what I'm thinking.

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


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