[cpp-threads] Should atomic_add support pointers?

Peter Dimov pdimov at mmltd.net
Sun Nov 6 20:55:54 GMT 2005


Doug Lea wrote:
> Peter Dimov wrote:
>> I'm trying to nail down the semantics of an atomic ops library and I
>> encountered the following interesting issue:
>>
>> Should atomic_add support pointer addition? That is:
>>
>> int * p;
>>
>> atomic_add( &p, 4 ); // allowed? sensible?
>>
>> What do you think?
>
> In
>   atomic_add( &p, 3 );
> would the "3" mean 3 or does it mean 12 = 3 * sizeof(int)?
> (Either way seems defensible.)

I'm interested in the p += 3 case (3 * sizeof(int) on a byte-addressable 
machine, just 3 on a hypothetical non-extinct word-addressable machine), 
because it has a direct language equivalent.

> Also I suspect that people like Maged Michael who use marked-pointers
> a lot would like to be able to do:
>    atomic_or(&p, (intptr_t)1);
>    atomic_and(&p, ~(intptr_t)1);

This certainly seems useful, but since it's inherently non-portable (or even 
less portable, if you like), the specification can leave its semantics (even 
more) implementation defined. Value bits vs storage bits, also an 
interesting issue. 




More information about the cpp-threads mailing list