Re: CSingleLock
- From: Ulrich Eckhardt <doomster@xxxxxxxx>
- Date: Thu, 27 Dec 2007 13:47:05 +0100
David Schwartz wrote:
On Dec 26, 7:19 pm, b...@xxxxxxxxxxxxxx wrote:
Can someone verify that the unlocking does indeed take place after
the return statement in this code? (i.e. after the access of myvar
is complete)
MyClass Foo::getfoo(void)
{
CSingleLock mylock(&csection,TRUE);
return m_myvar;
}
For the record, I have always included a coding policy prohibiting
this type of code in every coding standard document I have written.
This is just bad, bad, bad.
It is fine to have a lock holder class. But it should *assert* if the
lock is not release in debug code and only release the lock in release
code.
This makes it practically impossible to use exceptions. To elaborate on that
point, it costs all benefits of exceptions, i.e. that they propagate up the
stack automatically without cluttering code with check-and-return
statements, because you have to wrap any code that might throw with a mutex
locked in try-catch-clauses that unlock the mutex and rethrow. Also,
concerning the OP's code, it requires copying returnvalues to temporaries
only to then unlock the mutex and return the returnvalue. If you also ban
use of exceptions that might be okay, but then you are not using C++ to its
full capacity (not even close) anyway, which is a shame IMHO.
The main problem is simply that it is too easy to think you hold the
same locks before the ending '{' as you did after the beginning '}'.
Whereas, if you see:
{
CSingleLock foo(...);
// some code
foo.Unlock();
// some more code
return;
}
You now know you must make a choice whether to add code before the
'foo.Unlock' or after. It is clear that at '}' you hold the same locks
as you did at '{'.
A majority of my code works with the critical section spanning a whole
function. Otherwise, you can still limit the scope (i.e. the CSingleLock
object does have an Unlock() method).
Lastly, but that is beyond this discussion, I tend to bundle access to an
object with holding a mutex that guarantees that the access is exclusive.
This is achieved using a smart pointer approach and gives code that is
typically correct (i.e. you can't forget to lock the object, although you
still face the danger of e.g. deadlocks) but still easy to maintain.
I'd be interested to know the bigger picture of your statement above.
cheers
Uli
.
- Follow-Ups:
- Re: CSingleLock
- From: David Schwartz
- Re: CSingleLock
- References:
- CSingleLock
- From: bob
- Re: CSingleLock
- From: David Schwartz
- CSingleLock
- Prev by Date: Re: Writing a unittest against thread (un)safe ref-counter pointer
- Next by Date: Re: Writing a unittest against thread (un)safe ref-counter pointer
- Previous by thread: Re: CSingleLock
- Next by thread: Re: CSingleLock
- Index(es):
Relevant Pages
|
Loading