[Insight-developers] LightObject race condition fix

Gaëtan Lehmann gaetan.lehmann at jouy.inra.fr
Wed Jul 11 10:24:40 EDT 2007


Le 11 juil. 07 à 15:33, Karthik Krishnan a écrit :

> However I agree with Steve/Luis that access of the resource  
> m_ReferenceCount be thread safe. One solution would be to use a  
> scoped lock (unlocking is implemented in its destructor, so it  
> automatically does that when it goes out of scope).
>
> The code would look like
>
> UnRegister()
> {
>   ScopedLock lock( m_ReferenceCountLock )
>   m_ReferenceCount--;
>   if ( m_ReferenceCount <= 0) { delete this;  }
> }

Please correct if I'm wrong, but the mutex lock will be destroyed by  
the delete call before the destruction of the ScopedLock object, and  
so the ScopedLock object will try to release the lock on a destroyed  
mutex, exactly as with Luis' suggestion.

And it doesn't make Steve's example safe: a thread can still call the  
Register() method while a second thread is running the UnRegister()  
method which will destroy the object. The first thread will lock the  
mutex, the second one will destroy the object during the first thread  
lock. At this point, the behavior of the first thread become undefined.


>
> --------------------------------
>
> Would a scoped lock class that constructs itself from an  
> itk::SimpleFastMutexLock be useful ?
>
> Boost uses such a scoped lock in its implementation of shared_ptr.
>
> http://boost.cvs.sourceforge.net/boost/boost/boost/detail/ 
> shared_ptr_nmt.hpp?revision=1.5&view=markup
> http://boost.cvs.sourceforge.net/boost/boost/boost/detail/ 
> atomic_count_pthreads.hpp?revision=1.3&view=markup
>
> Thanks
> --k
>

--
Gaëtan Lehmann
Biologie du Développement et de la Reproduction
INRA de Jouy-en-Josas (France)
tel: +33 1 34 65 29 66    fax: 01 34 65 29 09
http://voxel.jouy.inra.fr



-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 186 bytes
Desc: =?ISO-8859-1?Q?Ceci_est_une_signature_=E9lectronique_PGP?=
Url : http://www.itk.org/mailman/private/insight-developers/attachments/20070711/86ef92f5/PGP.pgp


More information about the Insight-developers mailing list