Tuesday 15 February 2011

c# - Why use System.Threading.Interlocked.Decrement instead of minus? -


I changed some c # code on vb.net and converter. Tilik.com started it:

  i--;  

In this:

  System.Math.Max ​​(System.Threading.Interlocked.Decrement (i), i + 1)  

The current terminology for C # in code> i - is to return the current value of i (i.e., the value is before the decrease) and then a <<> code

So, we need to convert him to VB. We can not use i - = 1 because it does not return the current value of i before it decreases. Therefore, we need an operation that reduces i , but the value of i comes back to the value, like something:

  Function DoPostDecrement (I as integer) ByRef integer as I - = 1 return i + 1 termination function  

But to write a method to perform this To avoid being suggested to use the following:

  System.Math.Max ​​(someValueThatIsEqualToiMinusOne, someValueThatIsEqualtoiBeforeTheDecrement)  

but V B.NET will not use you to replace me - = 1 or i = i - 1 in the some valueIsquateTimionOn . However, System.Threading.Interlocked.Decrement (i) is valid and the value of i-1 is equal. Once you have done that, because the parameter is correct, the someValueThatIsEqualtoiBeforeTheDecrement is left to be evaluated to i + 1 (execute on that point decreasing i + 1 is the decreasing value.

Note that the above method is DoPostDecrement and System.Math.Max, System. Threading.Interlocked.Decrement creation can be different semantically in a multi-reference context.


No comments:

Post a Comment