问题描述
英特尔文档 说
该指令可以与 LOCK
前缀一起使用,以允许指令自动执行.
我的问题是
CMPXCHG
可以用内存地址操作吗?从文档看来不是,但谁能确认只适用于寄存器中的实际 VALUE,而不适用于内存地址?
Can
CMPXCHG
operate with memory address? From the document it seems not but can anyone confirm that only works with actual VALUE in registers, not memory address?
如果CMPXCHG
不是原子的,并且必须通过LOCK CMPXCHG
(使用LOCK
)实现高级语言级别的CAS前缀),引入这样的指令的目的是什么?
If CMPXCHG
isn't atomic and a high level language level CAS has to be implemented through LOCK CMPXCHG
(with LOCK
prefix), what's the purpose of introducing such an instruction at all?
(我是从高级语言的角度询问.即,如果在 x86 平台上必须将无锁算法翻译成 LOCK CMPXCHG,那么它仍然以 LOCK 为前缀.这意味着无锁算法是并不比那些精心编写的同步锁/互斥锁(至少在 x86 上)更好.这似乎也使得裸露的 CMPXCHG 指令毫无意义,因为我想引入它的主要目的是支持这种无锁操作.)
(I am asking from a high level language perspective. I.e., if the lock-free algorithm has to be translated into a LOCK CMPXCHG on the x86 platform, then it's still prefixed with LOCK. That means the lock-free algorithms are not better than ones with a carefully written synchronized lock / mutex (on x86 at least). This also seems to make the naked CMPXCHG instruction pointless, as I guess the major point for introducing it, was to support such lock-free operations.)
推荐答案
您将高级锁与碰巧名为 LOCK
的低级 CPU 功能混淆了.
You are mixing up high-level locks with the low-level CPU feature that happened to be named LOCK
.
无锁算法试图避免的高级锁可以保护执行可能需要任意时间的任意代码片段,因此,这些锁必须将线程置于等待状态,直到锁可用,这是一项代价高昂的操作,例如意味着维护一个等待线程的队列.
The high-level locks that lock-free algorithms try to avoid can guard arbitrary code fragments whose execution may take arbitrary time and thus, these locks will have to put threads into wait state until the lock is available which is a costly operation, e.g. implies maintaining a queue of waiting threads.
这与 CPU LOCK
前缀功能完全不同,它仅保护单个指令,因此可能仅在该单个指令的持续时间内保留其他线程.由于这是由 CPU 本身实现的,因此不需要额外的软件工作.
This is an entirely different thing than the CPU LOCK
prefix feature which guards a single instruction only and thus might hold other threads for the duration of that single instruction only. Since this is implemented by the CPU itself, it doesn’t require additional software efforts.
因此,开发无锁算法的挑战不是完全消除同步,而是将代码的关键部分减少到由 CPU 本身提供的单个原子操作.
Therefore the challenge of developing lock-free algorithms is not the removal of synchronization entirely, it boils down to reduce the critical section of the code to a single atomic operation which will be provided by the CPU itself.
这篇关于x86 CMPXCHG 是原子的,如果是,为什么需要 LOCK?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!