Why use System.Threading.Interlocked.Decrement instead of minus?

百般思念 提交于 2019-12-10 13:03:21

问题


I converted some c# code to vb.net and the converter.telerik.com turned this:

i--;

into this:

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

Whats up with all the fancy-ness?


回答1:


Michał Piaskowski's comment triggered the following explanation:

The semantics of i-- in C# are to return the current value of i (i.e., the value before the decrement occurs) and then decrement i by one.

So, we need to convert that to VB. We can not use i -= 1 because this does not return the current value of i before the decrement. So, we need an operation that will decrement i but return the value of i before the decrement, something like:

Function DoPostDecrement(ByRef i As Integer) As Integer
    i -= 1
    Return i + 1
End Function

But this suggests using the following to avoid having to write a method to perform the above:

System.Math.Max(
    someValueThatIsEqualToiMinusOne,
    someValueThatIsEqualtoiBeforeTheDecrement
)

But VB.NET won't let you use i -= 1 or i = i - 1 in place of someValueThatIsEqualToiMinusOne. However, System.Threading.Interlocked.Decrement(i) is legit and equal to the value of i - 1. Once you do that, because parameters are evaluated left to right, someValueThatIsEqualtoiBeforeTheDecrement should be i + 1 (at that point the decrement has been performed to i + 1 is the pre-decrement value.

Note that the above method DoPostDecrement and the System.Math.Max, System.Threading.Interlocked.Decrement construct could have different semantics in a multithreaded context.




回答2:


The Interlocked operation is atomic; in multithreaded contexts you can safely use it without holding a lock, if you're careful.




回答3:


The only reason I can see is from

Interlocked.Decrement Method

Decrements a specified variable and stores the result, as an atomic operation.




回答4:


It depends - is "i" a shared variable? Is it in a thread-safe environment?

If "i" is an integer, then i-- does essentially the following (ignoring the details):

  1. subtracts one from i
  2. assigns that value back to i

As you can see, there are > 1 steps. If "i" is in a non-thread-safe location (static variable shared across threads, etc), then the thread could potentially stop in the middle of those two steps, another thread could run both steps, and then you'd have a problem with invalid data.

The Interlocked class essentially combines the two steps above into a single step, providing an atomic operation. Now you don't have to worry about threads, since it's a single operation and can't be interrupted by another thread.




回答5:


To answer your question, looks like this converter.telerik.com thing is being overly-conservative about threading problems. WAAAY overly-conservative. I would revert the code to i-- if the same instance of i isn't being mutated from multiple threads concurrently.



来源:https://stackoverflow.com/questions/1828728/why-use-system-threading-interlocked-decrement-instead-of-minus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!