Boost logo

Boost :

Subject: Re: [boost] storing intrusive_ptr in atomic?!
From: Oliver Kowalke (oliver.kowalke_at_[hidden])
Date: 2013-10-25 04:11:39

2013/10/25 Andrey Semashev <andrey.semashev_at_[hidden]>

> intrusive_ptr is not trivial-copyable, precisely because it has to operate
> on
> the counter. Using memcpy for copying it is not correct.
For this reason it is
> not compatible with atomic<>. You have to use raw pointers with atomic<>.

I don't agree because the standard told us:

1.) 'For any object of trivially copyable type T, whether or not the object
holds a valid value of type T, the underlying bytes making up the object
can be copied into an array
of char or unsigned char. If the content of the array of char or unsigned
char is copied back into the
object, the object shall subsequently hold its original value.'

#define N sizeof(T)
char buf[N];
T obj;
std::memcpy(buf, &obj, N);
std::memcpy(&obj, buf, N);

this is correct for intrusive_ptr<T>.

2.) 'For any trivially copyable type T, if two pointers to T point to
distinct T objects obj1 and obj2, where
neither obj1 nor obj2 is a base-class subobject, if the underlying bytes
(1.7) making up obj1 are copied
into obj2,41 obj2 shall subsequently hold the same value as obj1.'

T* t1p;
T* t2p;
std::memcpy(t1p, t2p, sizeof(T));

this is correct for intrusive_ptr<T> too.

The only open question is: does the use-counter semantics prevent using
intrusive_ptr<T> together with atomic?.
you could increment the use-counter before and decrement the use-counter
after using it with atomic<>.

Boost list run by bdawes at, gregod at, cpdaniel at, john at