Why we need lock prefix before CMPXCHG [duplicate]

拥有回忆 提交于 2019-12-01 19:33:40

问题


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

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.

Mean to say if i dont use CMPXCHG the worst thing could happen is that i have to spin again.


回答1:


  • CMXCHG is truly atomic(asserts a bus lock) on a multiprocessor system only when it is prefixed with LOCK. 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.

Bottom line: Lock prefix makes the CMPXCHG a multiprocessor barrier instruction.



来源:https://stackoverflow.com/questions/17020128/why-we-need-lock-prefix-before-cmpxchg

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