Boost logo

Boost :

Subject: Re: [boost] storing intrusive_ptr in atomic?!
From: Giovanni Piero Deretta (gpderetta_at_[hidden])
Date: 2013-10-25 08:25:21


On Fri, Oct 25, 2013 at 1:12 PM, Giovanni Piero Deretta <gpderetta_at_[hidden]
> wrote:

> On Fri, Oct 25, 2013 at 12:43 PM, Oliver Kowalke <oliver.kowalke_at_[hidden]
> > wrote:
>
>> 2013/10/25 Giovanni Piero Deretta <gpderetta_at_[hidden]>
>>
>> > that's the tricky part. To adjust the reference count, you must load and
>> > dereference the pointer itself, but between those two operations another
>> > thread might come in, replace the pointer, adjust the original pointer
>> > count down and free the pointed object, together with the counter. You
>> need
>> > some way to defer destruction till a safe point (RCU, hazard pointers,
>> > etc).
>> >
>>
>> 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 the producer, a simple store with release semantics might actually be
> enough.
>

No, of course not, you can store only if there is already a null pointer
(or combine the produce/consume in a swap operation), so you need a CAS.

-- gpd


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