Boost logo

Boost Users :

Subject: [Boost-users] Boost.Atomic documentation, Reference Counting
From: Václav Zeman (vhaisman_at_[hidden])
Date: 2012-04-23 03:26:10


Hello.

Your documentation to Boost.Atomic ([1]) has been given me as an
answer to my StackOverflow ([2]) question. Here is a snippet from the
documentation:

  friend void intrusive_ptr_release(const X * x)
  {
    if (x->refcount_.fetch_sub(1, boost::memory_order_release) == 1) {
      boost::atomic_thread_fence(boost::memory_order_acquire);
      delete x;
    }
  }

And your rationale: "It is important to enforce any possible access to
the object in one thread (through an existing reference) to happen
before deleting the object in a different thread. This is achieved by
a "release" operation after dropping a reference (any access to the
object through this reference must obviously happened before), and an
"acquire" operation before deleting the object."

I wonder why this would not work well if it looked like this instead:

  friend void intrusive_ptr_release(const X * x)
  {
    if (x->refcount_.fetch_sub(1, boost::memory_order_relaxed) == 1) {
      boost::atomic_thread_fence(boost::memory_order_acq_rel);
      delete x;
    }
  }

[1] <http://www.chaoticmind.net/~hcb/projects/boost.atomic/doc/atomic/usage_examples.html#boost_atomic.usage_examples.example_reference_counters>
[2] <http://stackoverflow.com/questions/10268737/c11-atomics-and-intrusive-shared-pointer-reference-count>

-- 
VZ

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net