Boost logo

Boost :

Subject: [boost] storing intrusive_ptr in atomic?!
From: Oliver Kowalke (oliver.kowalke_at_[hidden])
Date: 2013-10-25 03:35:55


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?

Oliver


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