<HTML>
<HEAD>
<TITLE>Re: [vtk-developers] Weak pointers and thread safety</TITLE>
</HEAD>
<BODY>
<FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'>Oh, if I were only to believe it would be that simple.  Don’t forget that many objects rely on reference counting to determine whether they should actually be deleted.  I shudder to think what might happen if reference counts changed while a garbage collection was running.<BR>
<BR>
-Ken<BR>
<BR>
<BR>
On 10/12/10 7:36 AM, "David Gobbi" <<a href="david.gobbi@gmail.com">david.gobbi@gmail.com</a>> wrote:<BR>
<BR>
</SPAN></FONT><BLOCKQUOTE><FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'>Hi Ken,<BR>
<BR>
I see your point.  Indeed, applying a lock just to the WeakPointer<BR>
doesn't really do anything.  There might be a way to just put a lock<BR>
just around the object deletion decision in vtkObjectBase, without<BR>
having it around every decref.  Or there might not.<BR>
<BR>
   David<BR>
<BR>
On Tue, Oct 12, 2010 at 7:12 AM, Moreland, Kenneth <<a href="kmorel@sandia.gov">kmorel@sandia.gov</a>> wrote:<BR>
> I’m skeptical that this code actually provides thread safety.  Your critical<BR>
> section is only applied within a weak pointer.  It is not applied within<BR>
> vtkObjectBase itself.  While the critical section is running some other<BR>
> thread could call unreference and delete the object after the weak pointer<BR>
> checks the reference but before the smart pointer increments the reference.<BR>
><BR>
> I think the only way you are going to get true thread safety is to put a<BR>
> critical section somewhere in the register/unregister calls.  However, that<BR>
> might slow down everything appreciably.<BR>
><BR>
> -Ken<BR>
><BR>
><BR>
> On 10/11/10 10:43 PM, "Utkarsh Ayachit" <<a href="utkarsh.ayachit@kitware.com">utkarsh.ayachit@kitware.com</a>> wrote:<BR>
><BR>
> That sounds like a good idea. You have my vote.<BR>
><BR>
> Utkarsh<BR>
><BR>
> On Mon, Oct 11, 2010 at 10:35 PM, David Gobbi <<a href="david.gobbi@gmail.com">david.gobbi@gmail.com</a>> wrote:<BR>
>> Hi All,<BR>
>><BR>
>> The VTK weak pointer implementation is not thread safe because it can<BR>
>> be used just like a regular pointer, even though the object itself<BR>
>> might be deleted by a separate thread at any time.<BR>
>><BR>
>> By comparison, "safe" weak pointer like in boost and Qt cannot be used<BR>
>> in place of ordinary pointers.  Instead, in order to use them their<BR>
>> "GetPointer()"-like methods increment the reference count and then<BR>
>> return a smart pointer, which is guaranteed to be valid until it goes<BR>
>> out of scope:<BR>
>><BR>
>> vtkSmartPointer<SomeClass> smartPtr = weakPtr.GetPointer();<BR>
>> if (smartPtr.GetPointer() != 0)<BR>
>>  {<BR>
>>  // do something with smartPtr<BR>
>>  }<BR>
>> // reference count decremented when smartPtr goes out of scope<BR>
>><BR>
>> Because VTK's vtkWeakPointer::GetPointer() does not return a smart<BR>
>> pointer or do any thread locking, thread-safe use of vtkWeakPointer<BR>
>> requires the following:<BR>
>><BR>
>> vtkSimpleCriticalSection critSec;<BR>
>> critSec.Lock();<BR>
>> vtkSmartPointer<SomeClass> smartPtr = weakPtr.GetPointer();<BR>
>> critSec.Unlock();<BR>
>> if (smartPtr.GetPointer() != 0)<BR>
>>  {<BR>
>>  // do something with smartPtr<BR>
>>  }<BR>
>><BR>
>> If weakPtr.GetPointer() was changed to return a smart pointer, then it<BR>
>> could do the locking internally.  This change would be mostly, but not<BR>
>> completely, backwards compatible.  What do people think?<BR>
>><BR>
>>  David<BR>
>> _______________________________________________<BR>
>> Powered by www.kitware.com<BR>
>><BR>
>> Visit other Kitware open-source projects at<BR>
>> <a href="http://www.kitware.com/opensource/opensource.html">http://www.kitware.com/opensource/opensource.html</a><BR>
>><BR>
>> Follow this link to subscribe/unsubscribe:<BR>
>> <a href="http://www.vtk.org/mailman/listinfo/vtk-developers">http://www.vtk.org/mailman/listinfo/vtk-developers</a><BR>
>><BR>
>><BR>
> _______________________________________________<BR>
> Powered by www.kitware.com<BR>
><BR>
> Visit other Kitware open-source projects at<BR>
> <a href="http://www.kitware.com/opensource/opensource.html">http://www.kitware.com/opensource/opensource.html</a><BR>
><BR>
> Follow this link to subscribe/unsubscribe:<BR>
> <a href="http://www.vtk.org/mailman/listinfo/vtk-developers">http://www.vtk.org/mailman/listinfo/vtk-developers</a><BR>
><BR>
><BR>
><BR>
><BR>
><BR>
>    ****      Kenneth Moreland<BR>
>     ***      Sandia National Laboratories<BR>
> ***********<BR>
> *** *** ***  email: <a href="kmorel@sandia.gov">kmorel@sandia.gov</a><BR>
> **  ***  **  phone: (505) 844-8919<BR>
>     ***      web:   <a href="http://www.cs.unm.edu/~kmorel">http://www.cs.unm.edu/~kmorel</a><BR>
><BR>
><BR>
<BR>
<BR>
</SPAN></FONT></BLOCKQUOTE><FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'><BR>
</SPAN></FONT><FONT SIZE="2"><FONT FACE="Consolas, Courier New, Courier"><SPAN STYLE='font-size:10pt'><BR>
   ****      Kenneth Moreland<BR>
    ***      Sandia National Laboratories<BR>
***********  <BR>
*** *** ***  email: <a href="kmorel@sandia.gov">kmorel@sandia.gov</a><BR>
**  ***  **  phone: (505) 844-8919<BR>
    ***      web:   <a href="http://www.cs.unm.edu/~kmorel">http://www.cs.unm.edu/~kmorel</a><BR>
</SPAN></FONT></FONT><FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'><BR>
</SPAN></FONT>
</BODY>
</HTML>