You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Incorrect and unexpected optimizer behavior for Interlocked.Exchange with short value #7810
NOTE:Most likely this does not just affect Interlocked.Exchange, but it's easy to reproduce this way.
When turning on the optimization for the following code you get different (and also wrong) output values than from building the application without turning on the optimizer.
usingSystem;usingSystem.Threading;classProgram{privatestaticint_intToExchange=-1;privatestaticshort_innerShort=2;staticvoidMain(string[]args){varoldValue=Interlocked.Exchange(ref_intToExchange,_innerShort);Console.WriteLine("It was: {0}",oldValue);Console.WriteLine("It is: {0}",_intToExchange);Console.WriteLine("Expected: {0}",_innerShort);}}
Without using optimization the output is as expected:
It was: -1
It is: 2
Expected: 2
But when you turn on optimization (e.g. with <Optimize>true</Optimize> in .csproj) this is the output:
It was: 65535
It is: -65534
Expected: 2
More about this bug can also be found here on stackoverflow.
Activity
mikedn commentedon Apr 5, 2017
As far as I can tell this affects
Interlocked.AddandInterlocked.Exchange. Do you have other examples?stefan-schweiger commentedon Apr 5, 2017
I don't have any other examples, it was just my best guess that this would probably also affect other methods with similar signatures or internals.