本文介绍了为什么GCC使用mov/mfence而不是xchg来实现C11的atomic_store?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++和2012年后:Herb Sutter-原子武器,共2分之二 Herb Sutter认为(大约0:38:20)人们应该使用 xchg ,而不是 mov / mfence 在x86上实现 atomic_store .他似乎还暗示,这一特定的指令顺序是每个人都同意的顺序.但是,海湾合作委员会使用后者.为什么GCC使用此特定实现?

In C++ and Beyond 2012: Herb Sutter - atomic<> Weapons, 2 of 2 Herb Sutter argues (around 0:38:20) that one should use xchg, not mov/mfence to implement atomic_store on x86. He also seems to suggest that this particular instruction sequence is what everyone agreed one. However, GCC uses the latter. Why does GCC use this particular implementation?

推荐答案

很简单, mov mfence 方法更快,因为它不会触发冗余内存读取就像 xchg 一样,这会花费一些时间.x86 CPU始终保证线程之间严格的写入顺序,因此就足够了.

Quite simply, the mov and mfence method is faster as it does not trigger a redundant memory read like the xchg which will take time. The x86 CPU guarantees strict ordering of writes between threads anyway so so it is enough.

请注意,一些非常老的CPU在 mov 指令中存在一个错误,这使得 xchg 成为必需,但这是很久以前的解决方法了,不值得大多数用户的日常开销.

Note some very old CPUs have a bug in the mov instruction which makes xchg necessary but this is from a very long time ago and working around this is not worth the overhead to most users.

致谢@amdn,以获取有关旧的奔腾CPU中导致过去需要 xchg 的错误的信息.

Credit to @amdn for the information on the bug in old Pentium CPUs causing xchg to be needed in the past.

这篇关于为什么GCC使用mov/mfence而不是xchg来实现C11的atomic_store?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-27 20:55