atomic operations compared to c++

gzp via Digitalmars-d digitalmars-d at puremagic.com
Mon Jun 12 23:12:46 PDT 2017


I'm trying to port some simple lock-free algorithm to D and as 
the docs are quite minimal I'm stuck a little bit.

The memory order seem to be ok:
MemoryOrder.acq -> C++ accquire
MemoryOrder.rel -> C++ release
MemoryOrder.raw -> C++ relaxed
MemoryOrder.seq -> C++ seq_cst or acq_rel (the strongest)
There is no consume in D.

But what about compare_exchange (CAS) ? In C++ one have to 
provide Memory ordering for success and failure, but not in D. 
Does it mean, it is the strongest sequaential all the time, all 
some explicit fence have to be provided. Or the difference is 
that, CAS in D does not updates the expected value and in C++ the 
orderin is used for this update ? Thus in the usual spin loop I 
have to add an explicit fence on success?


ubyte flagsNow, newFlags;
do {
   flagsNow = atomicLoad!( MemoryOrder.acq )( flags_ );
   newFlags = update( flagsNow );
} while( !cas( &flags_, flagsNow, newFlags ) );
// do I need fence here ???


Another issue is the fence. In D there is no memoryordering for 
fence, only the strongest one exists. Is it intentional? (Not as 
if I have ever used explicit barriers apart from the one included 
in the atomic operations itself:) )

Thanks: Gzp



More information about the Digitalmars-d mailing list