问题描述
我正在阅读[[c0>描述]]) https://www.felixcloutier .com/x86/CMPXCHG.html ):
I'm reading about [lock cmpxchg
description]) https://www.felixcloutier.com/x86/CMPXCHG.html):
现在考虑两个执行lock cmpxchg
的线程:
Now consider two threads executing lock cmpxchg
:
Thread 1 Thread 2
mov ebx, 0x4000 mov ebx, 0x4000 ; address
mov edx, 0x62ab6 mov edx, 0x62ab8 ; new val
mov eax, 0x62ab1 mov eax, 0x62ab1 ; old
lock cmpxchg [ebx], eax lock cmpxchg [ebx], eax ; <----- here
问题是线程1和线程2中的锁定cmpxchg
都可能失败吗?
The question is can both lock'ed cmpxchg
in Thread 1 and Thread 2 fail?
因为
我猜这两个线程都可以具有写周期,而且由于与一个过时的值进行比较,因此两个线程都可以还原...但是我不确定这是否正确.
I could guess that both of the threads can have the write cycle and than both of them can be reverted because of comparing to a stale value... But I'm not sure if this is correct.
也许我需要看一下cas的实现细节,但是intel指令参考中没有指定它(至少我找不到)
Maybe I need to look at the cas implementation details, but it is not specified in the intel instruction reference (At least I could not find)
推荐答案
我的理解是,lock cmpxchg
不能虚假地失败-与LL/SC不同-假定内存地址上的值确实匹配.它通过获取缓存行的专有所有权,并在完成操作之前不将其交给其他内核,从而根据缓存一致性协议建立了这些保证.
My understanding is that lock cmpxchg
cannot fail spuriously - unlike LL/SC - assuming the value at the memory address indeed matches. It builds those guarantees from the cache coherency protocol by taking exclusive ownership of the cache line and not yielding it to other cores until the operation is done.
因此,只有其他一些线程写入内存位置,CAS才能对所有线程失败.
So CAS can only fail for all threads if some other thread wrote to the memory location.
这篇关于CAS是否可以对所有线程失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!