Subj : Re: A question about atomic_ptr To : comp.programming.threads From : Peter Dimov Date : Sun Apr 17 2005 12:20 am Joe Seigh wrote: > You're need to atomically load the object and count ptrs at the same > time so you know their associated with each other. This is a problem if > you're trying to avoid atomic double word loads because not all platforms > have it. Put an extra ptr to object in refcount object. The count object already contains an extra pointer. The problem is that, in general, it isn't the same as the object pointer. If I have something like struct derived: base1, base2 shared_ptr p( new derived ); shared_ptr p1( p ); shared_ptr p2( p ); the object pointers in p1 and p2 may not match the object pointer in p (which is also stored in the count). Looks like shared_ptr can't be strengthened without atomic DW ops (or locks). On the other hand... looks like it doesn't need to. shared_ptr * ppt; shared_ptr reader() { return *atomic_load_ddacq( ppt ); } void writer( ... ) { lock(); shared_ptr tmp( new T(**ppt) ); tmp->update( ... ); T * pp2 = new shared_ptr( tmp ); atomic_exchange_rel( ppt, pp2 ); delete pp2; unlock(); } .