当使用xchg时,我们需要mfence [英] Do we need mfence when using xchg

查看:237
本文介绍了当使用xchg时,我们需要mfence的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个设置并测试 xchg 的程序集锁。我的问题是:

I have a set and test xchg based assembly lock. my question is :

我们需要使用内存防护( mfence sfence lfence )?

Do we need to use memory fencing (mfence, sfence or lfence ) when using xchg instruction ?

编辑:

64位平台:搭配Intel nehalem

64 Bit platform : with Intel nehalem

推荐答案

正如在其他答案中所说的,锁前缀是隐式的,在这里,所以在汇编程序级别没有问题。当使用它作为内联汇编器时,问题可能在于C(或C ++)级别。在这里,你必须确保编译器不会对你的 xchg 重新排序指令。如果你正在使用gcc(或表兄弟),你通常会做类似的事情:

As said in the other answers the lock prefix is implicit, here, so there is no problem on the assembler level. The problem may lay on the C (or C++) level when you use that as inline assembler. Here you have to ensure that the compiler doesn't reorder instructions with respect to your xchg. If you are using gcc (or cousins) you would typically do something like:

  __asm__ __volatile__("xchgl %1, %0"
                       : "=r"(ret)
                       : "m"(*point), "0"(ret)
                       : "memory");

这是将指令声明为volatile

that is declare the instruction as volatile and add the "memory" clobber.

这篇关于当使用xchg时,我们需要mfence的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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