Boost logo

Boost :

From: Levente Farkas (lfarkas_at_[hidden])
Date: 2000-09-14 04:10:39


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>
  }
}
-----------
without lock/unlock is looks like:
-----------
{
  {
    boost::lock lock(m);
    int i = a.get();
  }
  while (i < 10)
  {
    <some code>
    f();
    <some code>
    {
      boost::lock lock(m);
      a.next();
      i = a.get();
    }
  }
}
-----------
IMHO the first version is more simple clear and easier to read and understand.
create the lock on the heap can't help here. yes it's true again that, we
can write it correctly we can use the current interface, but it's not the
same. I have to repeat myself, it's just another stupid example, probably there
are many others and these few lines examples never can be a real ones since one
can said in this case we can redesign the interface (eg. a.getfirst(i), and
a.getnext(i)) but the whole point is that, there are some case where lock/unlock
is useful.

 -- Levente
 "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