title: Fully thread-safe shared_ptr implementation
body: <p>Does anybody know of a fully thread-safe <code>shared_ptr</code> implementation? E.g. boost implementation of <code>shared_ptr</code> is thread-safe for the targets (refcounting) and also safe for simultaneous <code>shared_ptr</code> instance reads, but not writes or for read/write.</p>
  
  <p>(see <a href="http://www.boost.org/doc/libs/1_37_0/libs/smart_ptr/shared_ptr.htm#ThreadSafety" rel="noreferrer">Boost docs</a>, examples 3, 4 and 5).</p>
  
  <p>Is there a shared_ptr implementation that is fully thread-safe for <code>shared_ptr</code> instances?</p>
  
  <p>Strange that boost docs say that:</p>
  
  <blockquote>
    <p>shared_ptr objects offer the same level of thread safety as built-in types.</p>
  </blockquote>
  
  <p>But if you compare an ordinary pointer (built-in type) to <code>smart_ptr</code>, then simultaneous write of an ordinary pointer is thread-safe, but simultaneous write to a <code>smart_ptr</code> isn't.</p>
  
  <p>EDIT: I mean a lock-free implementation on x86 architecture.</p>
  
  <p>EDIT2: An example use case for such a smart pointer would be where there are a number of worker threads which update a global shared_ptr with a their current work item and a monitor thread that takes random samples of the work items. The shared-ptr would own the work item until another work item pointer is assigned to it (thereby destroying the previous work item). The monitor would get ownership of the work item (thereby preventing the work item to be destroyed) by assigning it to its own shared-ptr. It can be done with XCHG and manual deletion, but would be nice if a shared-ptr could do it.</p>
  
  <p>Another example is where the global shared-ptr holds a "processor", and is assigned by some thread, and used by some other thread. When the "user" thread sees that the processor shard-ptr is NULL, it uses some alternative logic to do the processing. If it's not NULL, it prevents the processor from being destroyed by assigning it to its own shared-ptr.</p>