multithreading - 在多核 x86 上,是否需要将 LOCK 作为 XCHG 的前缀?

标签 multithreading x86 atomic

如果 mem 是共享内存位置,我是否需要:

XCHG EAX,mem

或者:

LOCK XCHG EAX,mem

以原子方式进行交换?

谷歌搜索会得到"is"和“否”的答案。有谁明确知道这一点吗?

最佳答案

英特尔的文档似乎很清楚它是多余的。

IA-32 英特尔® 架构 软件开发人员手册 第 3A 卷: 系统编程指南,第 1 部分

7.1.2.1 说:

The operations on which the processor automatically follows the LOCK semantics are as follows:

  • When executing an XCHG instruction that references memory.

同样,

英特尔® 64 和 IA-32 架构 软件开发人员手册 第 2B 卷: 指令集引用,N-Z

XCHG:

If a memory operand is referenced, the processor’s locking protocol is automatically implemented for the duration of the exchange operation, regardless of the presence or absence of the LOCK prefix or of the value of the IOPL.

请注意,这实际上并不意味着无论是否使用 LOCK 前缀,都会断言 LOCK# 信号,7.1.4 描述了如果内存位置被缓存,则在后续处理器上如何在没有 LOCK# 的情况下保留锁定语义。聪明,绝对超出我的想象。

关于multithreading - 在多核 x86 上,是否需要将 LOCK 作为 XCHG 的前缀?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3144335/

相关文章:

java - 输入字符串到整数的数字格式异常

c# - 异步Socket.Begin接收内部调用之谜

assembly - orpd等SSE2指令的意义何在?

c# - 如何在 C# 中以原子方式交换 2 个整数?

java - 使用 Java 原子类进行模块化增量

java守护线程

Android 每 X 秒运行一次服务线程

assembly - Intel x86汇编手册中的00+是什么意思?

assembly - 什么是 callq 指令?

python - python中主线程和子线程之间的原子操作