需要支持以了解方法Interlocked.CompareExchange [英] need support to understand method Interlocked.CompareExchange

查看:553
本文介绍了需要支持以了解方法Interlocked.CompareExchange的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要支持来了解Interlocked.CompareExchange方法的工作方式……如何比较值...

谢谢.....

I need support to understand the working of method Interlocked.CompareExchange........that how it compare the values...

Thanks.....

推荐答案

.NET System.Threading.Interlocked类提供了一些非常有用的方法来帮助编写线程安全代码.比较复杂的方法之一是CompareExchange,它可用于计算可以从多个线程更新的运行总计. Interlocked.CompareExchange旨在通过处理器直接提供的本机原子指令来实现.在内部使用类似的锁是没有意义的.

任何具有锁定前缀的x86指令都具有完整的内存屏障. API和CompareExchanges使用锁定前缀的指令,例如锁定cmpxchg.因此,这意味着内存屏障.Interlocked.CompareExchange使用内存屏障.

对于P6系列处理器,锁定操作会将所有未完成的加载和存储操作序列化,即等待它们完成.对于Pentium 4和Intel Xeon处理器,该规则也适用,但有一个例外.引用弱排序的内存类型(例如WC内存类型)的加载操作可能无法序列化.
.NET System.Threading.Interlocked class provides some very useful methods to assist in writing thread-safe code. One of the more complex methods is CompareExchange, which can be used for computing a running total that may be updated from multiple threads. Interlocked.CompareExchange is meant to be implemented with native atomic instructions provided directly by the processor. It''s pointless to have something like that use a lock internally.

Any x86 instruction that has lock prefix has full memory barrier. APIs and CompareExchanges use lock-prefixed instruction such as lock cmpxchg. So, it implies memory fence.Interlocked.CompareExchange uses a memory barrier.

For the P6 family processors, locked operations serialize all outstanding load and store operations i.e. wait for them to complete. This rule is also true for the Pentium 4 and Intel Xeon processors, with one exception. Load operations that reference weakly ordered memory types (such as the WC memory type) may not be serialized.


这篇关于需要支持以了解方法Interlocked.CompareExchange的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆