Boost logo

Boost :

Subject: Re: [boost] Using Boost.Atomic for shared_ptr
From: Peter Dimov (pdimov_at_[hidden])
Date: 2009-12-21 15:31:32


Helge Bahmann wrote:

>> boost::atomic<long> use_count_; // #shared
>> boost::atomic<long> weak_count_; // #weak + (#shared != 0)
>
> wouldn't "int" do?

Yes it will. There aren't many 16 bit platforms left. :-)

> I would suggest:
>
> bool add_ref_lock() // true on success
> {
> long c = use_count_.load(memory_order_relaxed);
> do {
> if (c == 0) return false;
> } while(!use_count_.compare_exchange_weak(c, c+1,
> memory_order_relaxed));
> return true;
> }
>
> I have the nagging thought in my mind that the last one must
> be "memory_order_acquire" but I am too tired to really convince me
> either way :(

Relaxed is fine for add_ref. The decrements need to be acqrel. use_count()
needs acquire.


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