[vtk-developers] Weak pointers and thread safety

David Gobbi david.gobbi at gmail.com
Tue Oct 12 09:10:51 EDT 2010


Adding the critical section lock would add a few hundred CPU cycles or
so, i.e. significant if done inside hot loop, but insignificant
compared to things like pipeline updates.

Instead of changing how vtkWeakPointer::GetPointer() works, I could
add a new thread-safe method called vtkWeakPointer::GetSmartPointer().
 That way people could choose for themselves which method to use.

  David

On Tue, Oct 12, 2010 at 4:31 AM, Will Schroeder
<will.schroeder at kitware.com> wrote:
> How would this impact performance? Would it improve it do you think?
> W
>
>
> On Mon, Oct 11, 2010 at 10:35 PM, David Gobbi <david.gobbi at gmail.com> wrote:
>>
>> Hi All,
>>
>> The VTK weak pointer implementation is not thread safe because it can
>> be used just like a regular pointer, even though the object itself
>> might be deleted by a separate thread at any time.
>>
>> By comparison, "safe" weak pointer like in boost and Qt cannot be used
>> in place of ordinary pointers.  Instead, in order to use them their
>> "GetPointer()"-like methods increment the reference count and then
>> return a smart pointer, which is guaranteed to be valid until it goes
>> out of scope:
>>
>> vtkSmartPointer<SomeClass> smartPtr = weakPtr.GetPointer();
>> if (smartPtr.GetPointer() != 0)
>>  {
>>  // do something with smartPtr
>>  }
>> // reference count decremented when smartPtr goes out of scope
>>
>> Because VTK's vtkWeakPointer::GetPointer() does not return a smart
>> pointer or do any thread locking, thread-safe use of vtkWeakPointer
>> requires the following:
>>
>> vtkSimpleCriticalSection critSec;
>> critSec.Lock();
>> vtkSmartPointer<SomeClass> smartPtr = weakPtr.GetPointer();
>> critSec.Unlock();
>> if (smartPtr.GetPointer() != 0)
>>  {
>>  // do something with smartPtr
>>  }
>>
>> If weakPtr.GetPointer() was changed to return a smart pointer, then it
>> could do the locking internally.  This change would be mostly, but not
>> completely, backwards compatible.  What do people think?
>>
>>  David
>> _______________________________________________
>> Powered by www.kitware.com
>>
>> Visit other Kitware open-source projects at
>> http://www.kitware.com/opensource/opensource.html
>>
>> Follow this link to subscribe/unsubscribe:
>> http://www.vtk.org/mailman/listinfo/vtk-developers
>>
>
>
>
> --
> William J. Schroeder, PhD
> Kitware, Inc.
> 28 Corporate Drive
> Clifton Park, NY 12065
> will.schroeder at kitware.com
> http://www.kitware.com
> (518) 881-4902
>



More information about the vtk-developers mailing list