是x86 CMPXCHG原子吗? [英] Is x86 CMPXCHG atomic?

查看:561
本文介绍了是x86 CMPXCHG原子吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://www.intel.com/content/dam/www/public /us/en/documents/manuals/64-ia-32-architectures-software-developer-instruction-set-reference-manual-325383.pdf

该指令可以与LOCK前缀一起使用,以允许以原子方式执行指令。

"This instruction can be used with a LOCK prefix to allow the instruction to be executed atomically."

我的问题是


  1. CMPXCHG可以使用内存地址吗?从文档中似乎不是,但是任何人都可以确认,只有在寄存器中的实际值,而不是内存地址吗?

  1. 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?


推荐答案

您正在混合高级锁与正好命名为 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原子吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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