[cpp-threads] Yet another visibility question

Peter Dimov pdimov at mmltd.net
Mon Dec 11 23:20:01 GMT 2006


Boehm, Hans wrote:

> Peter - With these changes, are you convinced that reference counting
> can be efficiently implemented?  I haven't thought it through.

It seems that it can. We've already decided to not introduce acquire/release 
for loads or stores, so a single fetchadd_release is probably optimal under 
the current model. I have one question, though. Consider this example:

// v initially 2

// thread 1

x = 1;
fetchadd_release( &v, -1 );

// thread 2

if( fetchadd_release( &v, -1 ) == 1 ) // is true
{
    load_acquire( &v ); // dummy load to order the assert
    assert( x == 1 );
}

Will the assert pass under the new rules?

To provide context, Example 4 in your post represents reference counting for 
immutable objects, where we only need to make sure that the earlier readers 
don't see an object that is in the process of being destroyed. For mutable 
objects, we also need to make sure that the destructor in thread 2 sees the 
modifications to the object from thread 1. There are several ways to write 
the decrement in this case:

1. fetchadd_ordered
2. as above
3. fetchadd_release; on last reference, fetchadd_acquire( &v, 0 );
4. if v == 1 compare_exchange_acquire( &v, 1, 0 ) else 
compare_exchange_release( &v, old, old-1 ).

As an aside, this is one place where the hardware details leak through our 
abstract atomics since we don't know which alternative will be optimal.

Anyway, I was just wondering whether (2) is a legitimate option, or we need 
(3). It seems to me that the latest changes make (2) work. 




More information about the cpp-threads mailing list