Boost logo

Boost :

From: Levente Farkas (lfarkas_at_[hidden])
Date: 2000-09-15 04:49:50


Branko Èibej wrote:
>
> Levente Farkas wrote:
> >
> > hi,
> > I find an example where lock and unlock can be useful.
> > suppose we'd like to protect a with m, but we don't would like to protect f
> > (suppose it's long) and have the following code (if we have lock/unlock on
> > mutex).
> > -----------
> > {
> > boost::lock lock(m);
> > for (int i = a.get(); i < 10; a.next())
> > {
> > <some code>
> > m.unlock()
> > f();
> > m.lock();
> > <some code>
> > }
> > }
>
> I think this is a real problem, but you don't need to expose
> lock() and unlock() to solve it. Instead you have a lock-inverter
> class (which must be a friend of lock, yes, but I think that's
> justified in this case):
>
> namespace boost {
> class invert_lock
> {
> public:
> invert_lock (lock& l)
> : lock_ref(l)
> {
> l.unlock();
> }
>
> ~invert_lock()
> {
> l.lock();
> }
> private:
> lock& lock_ref;
> };
> }
>
> Your example becomes:
>
> {
> boost::lock lock(m);
> for (int i = a.get(); i < 10; a.next())
> {
> <some code>
> {
> boost::invert_lock unlock(lock);
> f();
> }
> <some code>
> }
> }

yes it's a solution, which is another solution for ONE problem, but if we
can use it we loose compile time chech whcih is so much important for
william, and other things. so keep on go around the lock/unlock function
(as I sad earlier the lock/unlock can be on the lock class).

 -- Levente http://petition.eurolinux.org/index_html
 "The only thing worse than not knowing the truth is
  ruining the bliss of ignorance."


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