CAS是否可以对所有线程失败? [英] Can CAS fail for all threads?

查看:130
本文介绍了CAS是否可以对所有线程失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读[[c0>描述]]) https://www.felixcloutier .com/x86/CMPXCHG.html ):

I'm reading about [lock cmpxchg description]) https://www.felixcloutier.com/x86/CMPXCHG.html):

此指令可以与LOCK前缀一起使用以允许 原子执行的指令.为了简化 与处理器总线的接口,目标操作数 接收一个写周期,而不考虑结果 比较.如果以下情况写回目标操作数 比较失败;否则,源操作数为 写入目的地. (处理器从不 产生锁定的读取而没有产生锁定 写.)

This instruction can be used with a LOCK prefix to allow the instruction to be executed atomically. To simplify the interface to the processor’s bus, the destination operand receives a write cycle without regard to the result of the comparison. The destination operand is written back if the comparison fails; otherwise, the source operand is written into the destination. (The processor never produces a locked read without also producing a locked write.)

现在考虑两个执行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?

因为

目标操作数 接收一个写周期,而不考虑结果 比较

the destination operand receives a write cycle without regard to the result of the comparison

我猜这两个线程都可以具有写周期,而且由于与一个过时的值进行比较,因此两个线程都可以还原...但是我不确定这是否正确.

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是否可以对所有线程失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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