[M3devel] lock before or after try?

Roland Illig roland.illig at gmx.de
Fri Aug 21 21:44:50 CEST 2009


Jay K schrieb:
> lock
> try
>   stuff
> finally
>   unlock
> end
> 
> or
> try
>   lock
>   stuff
> finally
>   unlock
> end
> 
> Lock can't fail so no difference?

I would always choose the first variant, because if locking fails for 
some reason (although it cannot, I know), unlock will not be called 
without a corresponding lock.

At least in Java, it may happen that another thread interrupts this one 
just in the very nanosecond between the try and lock, throwing an 
exception, and then you would unlock something that hasn't been locked 
before.

Roland



More information about the M3devel mailing list