|
Boost Users : |
From: Jason Winnebeck (yg-boost-users_at_[hidden])
Date: 2003-07-31 17:05:49
Mark Sizer wrote:
> After much debate, we adopted the following coding standard:
>
> { boost::mutex::scoped_lock lock(_mtxShared);
>
> // do whatever
>
> }
That standard is great. RAII is great and I love it. I'm not using
boost threads, but I would have had I known about them at the time. I
have working code so I'm not going to change it.
I use exceptions in my program as well, so I use something that works
(from that example) identical to the scoped_lock. I usually do
something like:
void Object::function() {
LockMutex lock( mtx );
//whatever
}
Then it is exception and "return" safe. I have thought about using the
style you give all of the time, but I only do it now if I must (because
I want the mutex to lock early). The drawback is that you can't do
something like this with the RAII methods.
mtx.lock();
mtx2.lock();
mtx.release();
mtx2.release();
But I've never seen a case so far where I'm required to do that. I do
have a LockMutexEx object that allows you to call release to release
early. I make sure to use it as rarely as possible, but I've used it I
believe once on a condition variable where I wanted to keep it locked if
my condition was met. I doesn't sound like I'd need it when describing
it here but I did use it once. I can see a problem with it since with
RAII, you are supposed to "have the resource" while the object exists.
Jason
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net