|
Boost : |
Subject: Re: [boost] storing intrusive_ptr in atomic?!
From: Andrey Semashev (andrey.semashev_at_[hidden])
Date: 2013-10-25 03:41:51
On Friday 25 October 2013 09:35:55 Oliver Kowalke wrote:
> Hello,
>
> do you confirm that it is valid to store a intrusive_ptr inside an atomic?!
>
> struct X {
> std::size_t use_count;
> X() : use_count( 0) {}
> friend inline void intrusive_ptr_add_ref( X * p) BOOST_NOEXCEPT
> { ++p->use_count; }
> friend inline void intrusive_ptr_release( X * p)
> { if ( 0 == --p->use_count) delete p; }
> };
>
> X::ptr_t x1( new X()
> );
>
> X::ptr_t x2( new X()
> );
>
> boost::atomic< X::ptr_t > av1(
> x1);
>
> BOOST_ASSERT( av1.compare_exchange_strong( x1, x2) );
> BOOST_ASSERT( av1.load() != x1);
> BOOST_ASSERT( av1.load() == x2);
>
> A type T used in generic template atomic<T> is required to be trivial
> copy-able, e.g.
> I think it is trivial copy-able (std::memcpy) only the use_count will not
> incremented.
> The memory layout of intrusive_ptr is a raw-pointer so trivial copy-able
> should be correct.
>
> What do you think?
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<>.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk