为什么我们需要在CMPXCHG之前添加锁前缀 [英] Why we need lock prefix before CMPXCHG
问题描述
为什么intel体系结构中的CMPXCHG之前需要锁定前缀. 请参阅以供参考 http://courses.engr.illinois .edu/ece390/archive/spr2002/books/labmanual/inst-ref-cmpxchg.html
why do we need lock prefix before CMPXCHG in intel architecture. please see for reference http://courses.engr.illinois.edu/ece390/archive/spr2002/books/labmanual/inst-ref-cmpxchg.html
我不确定如果不使用锁会有什么后果.因为在将值加载到eax和执行LOCK CMPXCHG之间,可以更改该值而与锁定前缀无关,因为将值加载到eax和LOCK CMPXCHG是两条指令.
what i am not sure what are the consequences if don,t use lock. because between loading the value into eax and exceuting LOCK CMPXCHG the value could be changed irrespective of the lock prefix because loading value into eax and LOCK CMPXCHG are two instructions.
要说的是,如果我不使用CMPXCHG,可能会发生的最糟糕的事情是我必须再次旋转.
Mean to say if i dont use CMPXCHG the worst thing could happen is that i have to spin again.
推荐答案
-
仅当
-
CMXCHG
带有LOCK
前缀时,它才是多处理器系统上的真正原子(声明总线锁定).如今,正在使用基于侦听的缓存一致性协议,从而消除了对隔离(总线锁定)的需求. - 来到问题的第二部分(该值将始终被更改).是的,在这种情况下,
CMPXCHG
指令失败,但对于所有处理器而言仍然是原子的. CMXCHG
is truly atomic(asserts a bus lock) on a multiprocessor system only when it is prefixed withLOCK
. These days snooping based cache coherence protocols are being used which eliminates the need for the fence(bus lock).- Coming to second part of the question(the value would anyways be changed). YES in that case the
CMPXCHG
instruction fails but nevertheless is still atomic with respect to all the processors.
底线:锁定前缀使CMPXCHG
成为多处理器屏障指令.
Bottom line: Lock prefix makes the CMPXCHG
a multiprocessor barrier instruction.
这篇关于为什么我们需要在CMPXCHG之前添加锁前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!