Boost logo

Boost :

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


On Fri, Oct 25, 2013 at 12:10 PM, Oliver Kowalke
<oliver.kowalke_at_[hidden]>wrote:

> 2013/10/25 Giovanni Piero Deretta <gpderetta_at_[hidden]>
>
> > char buf[N];
> > T obj;
> > std::memcpy(buf, &obj, N);
> > std::memcpy(&obj, buf, N);
> >
> > For T = boost::intrusive_ptr<T2>, the above code formally leads to UB, as
> > intrusive_ptr is not trivially copiable.
> >
> > It might (or not) work in practice, but in the case of std::atomic, it
> will
> > bypass the copy constructor which means that the reference count won't be
> > updated.
> >
>
> the use-count can be 'adjusted' before, e.g. prevent deallocating the
> object owned by the intrusive_ptr unintentionally.
>
>
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).

-- gpd


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