Boost logo

Boost :

Subject: Re: [boost] storing intrusive_ptr in atomic?!
From: Peter Dimov (lists_at_[hidden])
Date: 2013-10-25 08:38:52


Oliver Kowalke wrote:
> what about this
>
> // use-counter in T is an atomic too
> array< atomic< intrusive_ptr< T > >, 100 > a;
>
> intrusive_ptr< T > p(...);
> intrusive_ptr_add_ref( p.get() );
> // add an intrusive_ptr to the array
> if ( a[index].compare_exchange_strong(null_ptr,p) ) {
> // successful added
> }
>
> // remove an interusive_ptr from array
> intrusive_ptr< T > e; // points to nothing
> if ( ! a[index].compare_exchange_strong(e, null_ptr) ) {
> // successfull removed
> intrusive_ptr_release( e.get() );
> }

For this, you don't seem to need atomic<intrusive_ptr> though; atomic<T*>
works just as well, doesn't it?

array< atomic< T* >, 100 > a;

intrusive_ptr< T > p(...);
// add an intrusive_ptr to the array
intrusive_ptr_add_ref( p.get() );
if ( a[index].compare_exchange_strong(null_ptr,p.get()) ) {
  // successfully added
}
else
{
    intrusive_ptr_release(p.get());
    // failed to add
}

// remove an intrusive_ptr from array
T* e = 0;
if ( ! a[index].compare_exchange_strong(e, null_ptr) ) {
    // successfully removed
    intrusive_ptr<T> e2( e, false );
    // return e2;
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk