Boost logo

Boost :

Subject: [boost] Using Boost.Atomic for shared_ptr
From: Phil Endecott (spam_from_boost_dev_at_[hidden])
Date: 2009-12-20 14:16:03

Dear All,

I have just had a quick look at how easily shared_ptr could be adapted
to use Helge's proposed Boost.Atomic. To me it looks very simple, but
maybe someone who knows more about all this could have a look? I took
the non-threaded implementation in sp_counted_base_nt.hpp as a starting
point, and simply changed the two counts from long to
boost::atomic<long> :

     boost::atomic<long> use_count_; // #shared
     boost::atomic<long> weak_count_; // #weak + (#shared != 0)

Then, the majority of the methods don't need to change since the atomic
template provides atomic versions of e.g. operator++. The one that
does need more work is add_ref_lock, which I've implemented as follows:

     bool add_ref_lock() // true on success
         while (1) {
             long c = use_count_;
             if (c == 0) return false;
             if (use_count_.compare_exchange_weak(c,c+1)) return true;

There is also the question of memory barriers. I understand that
things like operator++ provide the most pessimistic memory barriers, so
ideally these should be replaced with calls that use the right barriers.

Does anyone have any thoughts about how this would compare with the
current custom assembler implementations?

Cheers, Phil.

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