I am writing intrusive shared pointer and I am using C++11 <atomic>
facilities for reference counter. Here are the relevant fragments of my code:
//...
mutable std::atomic<unsigned> count;
//...
void
SharedObject::addReference() const
{
std::atomic_fetch_add_explicit (&count, 1u,
std::memory_order_consume);
}
void
SharedObject::removeReference() const
{
bool destroy;
destroy = std::atomic_fetch_sub_explicit (&count, 1u,
std::memory_order_consume) == 1;
if (destroy)
delete this;
}
I have started with memory_order_acquire
and memory_order_release
first but then I convinced myself that memory_order_consume
should be good enough. After further deliberation it seems to me that even memory_order_relaxed
should work.
Now, the question is whether I can use memory_order_consume
for the operations or could I use weaker ordering (memory_order_relaxed
) or should I use stricter ordering?