Compiler optimization breaks multi-threaded code

Sean Kelly sean at invisibleduck.org
Tue Nov 16 09:09:56 PST 2010


Kagamin <spam at here.lot> wrote:
> Sean Kelly Wrote:
> 
>>> shared uint cnt;
>>> void atomicInc  ( ) { uint o; while ( !cas( &cnt, o, o + 1 ) ) o =
> > > cnt; }
>>> 
>>> is compiled with dmd -O to something like:
>>> 
>>> shared uint cnt;
>>> void atomicInc  ( ) { while ( !cas( &cnt, cnt, cnt + 1 ) ) { } }
>> What a mess.  DMD isn't supposed to optimize across asm blocks.
> 
> There're no asm blocks in the code. It's a violated contract of shared
> data access.

cas() contains an asm block. Though I guess in this case the compiler
isn't actually optimizing across it. Does atomic!"+="(&cnt, 1) work
correctly?  I know the issue with shared would still have to be fixed,
but that code uses asm for the load as well, so it probably won't be
optimized the same way.


More information about the Digitalmars-d mailing list