Compiler optimization breaks multi-threaded code

Sean Kelly sean at invisibleduck.org
Sun Nov 14 07:31:08 PST 2010


Michal Minich Wrote:

> There is one question on SO which seems like a serious problem for atomic
> ops.
> 
> http://stackoverflow.com/questions/4165149/compiler-optimization-breaks-
> multi-threaded-code
> 
> in short:
> 
> 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 ) ) { } }
> 
> see the web page for details.

What a mess.  DMD isn't supposed to optimize across asm blocks.


More information about the Digitalmars-d mailing list