Subj : Re: A question about atomic_ptr To : comp.programming.threads From : Chris Thomasson Date : Sun Apr 10 2005 08:05 am "Peter Dimov" wrote in message news:d3b9b5$qea$1@domitilla.aioe.org... > Chris Thomasson wrote: >>> As a practical matter, I'd think you'd want some mechanism to >>> distinguish between shared global pointers and non-shared local >>> pointers. >> >> Agreed. For instance, I had to separate my atomic pointer into two >> distinguishable classes to address a highly critical performance >> issue: >> shared_smr_ptr >> local_smr_ptr >> >> >> shared_smr_ptr alone is "way to expensive" to be used for a general >> purpose smart pointer. > > That's what people tell me all the time, that shared_ptr is too expensive > and they need an "unsynchronized" variant. The overhead generated by the strict memory barrier loop required to access the reference count wrt shared_smr_ptr's really do require a "local access" class of sorts. Just something that avoids the SMR API for every reference count increment... :O > I think I'll reserve the local_ptr name for a pointer > that uses a "local" count and non-atomic updates (but is convertible from > and to a shared_ptr) so that people are free to shoot themselves in the > foot, if they so insist. That would be a pseudo distributed count. each local_ptr would contain a "separate" count. When a shared_ptr gets copied into a local_ptr an atomic increment with acquire semantics would be applied to the "shared" count, and the "local" count would be set to 1. The acquire semantics here would be used to sync with the "local_ptr drop to zero transition". When the "local" count drops to zero an atomic decrement with release semantics would be applied to the shared count to ensure that all the local affects on shared memory are applied before the decrement of the reference count. local_ptr = shared_ptr ------------------------------- atomic_inc_acquire( &shared_ptr::refs ); local_ptr::refs = 1; local_ptr = 0 ------------------------------- --local_ptr::refs if ( ! local_ptr::refs ) { if ( ! atomic_dec_acquire( &shared_ptr::refs ) ) { // whatever } } I could see where this would be useful wrt performance. It actually might be worth the trouble and the extra documentation... ;) .