问题描述
Interlocked.Increment
似乎是其中最标准/简单的操作之一,将需要在多线程代码来执行。
Interlocked.Increment
seems like among the most standard/simple of operations one would need to perform in multithreaded code.
我假设方法的功能是某种模式,只要有螺纹的经验将能够复制。
I assume that the functionality of the method is some sort pattern that anyone with threading experience would be able to replicate.
所以基本上是我想知道,如果有人可以提供一个精确副本(与它是如何工作的解释)什么样的 Interlocked.Increment
方法实际上做内部? (我看过的实际方法的来源,但一直无法找到它)
So basically what I am wondering is if someone could provide an exact duplicate (with explanation of how it works) of what the Interlocked.Increment
method is actually doing internally? (I have looked for the source of the actual method but been unable to find it)
推荐答案
据先生的它做两件事情:
According to Mr Albahari it does two things:
- 生成
全栅栏
限制互锁的重新排序和缓存瓦尔
使得公知的OS和虚拟机操作的原子,从而使例如在32位系统上的64位值的操作将是原子
- makes the atomicity of the operation known to the OS and VM, so that e.g. operations on 64bit values on 32bit system will be atomic
- generates
full fence
restricting reordering and caching of the Interlocked vars
有一个看看这个链接 - 它提供了一些很好的例子。
Have a look at that link - it gives some nice examples.
这篇关于什么是Interlocked.Increment实际上在做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!