[Insight-users] Re: Insight-users Digest, Vol 43, Issue 7

Julia Smith julia.smith at ondiagnostics.com
Mon Nov 5 15:07:55 EST 2007


3.4.0
I haven't built it yet, but did take a look at itkLightObject. Now  UnRegister does correct multi-threading thing of _not_  referencing a volatile value outside a semaphore, SetRegister() should do the same thing. It's just not thread safe to check values modifiable by another context. A race condition persists if one thread sets a reference count to zero and another decrements it to zero, both may attempt to delete the same piece of memory. A true pain to debug.

void
LightObject
::UnRegister() const
{
  m_ReferenceCountLock.Lock();
  int tmpReferenceCount = --m_ReferenceCount;
  m_ReferenceCountLock.Unlock();

  // ReferenceCount in now unlocked.  We may have a race condition
  // to delete the object.
  if ( tmpReferenceCount <= 0)
    {
    delete this;
    }
}


void
LightObject
::SetReferenceCount(int ref)
{
  m_ReferenceCountLock.Lock();
  m_ReferenceCount = ref;
  m_ReferenceCountLock.Unlock();

  if ( m_ReferenceCount <= 0)
    {
    delete this;
    }
}


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://public.kitware.com/pipermail/insight-users/attachments/20071105/a25697c4/attachment.html


More information about the Insight-users mailing list