什么是内存栅栏?
使用显式内存栅栏是什么意思?
What is meant by using an explicit memory fence?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
使用显式内存栅栏是什么意思?
What is meant by using an explicit memory fence?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(5)
为了提高性能,现代 CPU 通常会乱序执行指令,以最大限度地利用可用芯片(包括内存读/写)。 由于硬件强制执行指令完整性,因此您在单个执行线程中永远不会注意到这一点。 然而,对于具有易失性内存(例如内存映射 I/O)的多线程或环境,这可能会导致不可预测的行为。
内存栅栏/屏障是一类指令,意味着内存读/写按照您期望的顺序发生。 例如,“完整栅栏”意味着栅栏之前的所有读/写都在栅栏之后的读/写之前提交。
注意内存栅栏是一个硬件概念。 在高级语言中,我们习惯于处理互斥体和信号量 - 这些很可能使用低级内存栅栏来实现,并且不需要显式使用内存屏障。 使用内存屏障需要仔细研究硬件架构,并且在设备驱动程序中比应用程序代码更常见。
CPU 重新排序与编译器优化不同 - 尽管结果可能相似。 如果这可能导致不良行为(例如在 C 中使用 volatile 关键字),您需要采取单独的措施来阻止编译器对指令重新排序。
For performance gains modern CPUs often execute instructions out of order to make maximum use of the available silicon (including memory read/writes). Because the hardware enforces instructions integrity you never notice this in a single thread of execution. However for multiple threads or environments with volatile memory (memory mapped I/O for example) this can lead to unpredictable behavior.
A memory fence/barrier is a class of instructions that mean memory read/writes occur in the order you expect. For example a 'full fence' means all read/writes before the fence are comitted before those after the fence.
Note memory fences are a hardware concept. In higher level languages we are used to dealing with mutexes and semaphores - these may well be implemented using memory fences at the low level and explicit use of memory barriers are not necessary. Use of memory barriers requires a careful study of the hardware architecture and more commonly found in device drivers than application code.
The CPU reordering is different from compiler optimisations - although the artefacts can be similar. You need to take separate measures to stop the compiler reordering your instructions if that may cause undesirable behaviour (e.g. use of the volatile keyword in C).
将我的答案复制到另一个问题,处理器有哪些技巧优化代码?:
Copying my answer to another question, What are some tricks that a processor does to optimize code?:
根据我的经验,它指的是内存屏障,这是一条用于同步的指令(显式或隐式)多个线程之间的内存访问。
问题出现在现代激进编译器(它们具有惊人的自由度来重新排序指令,但通常对线程一无所知)和现代多核 CPU 的组合中。
对这个问题的一个很好的介绍是“The 'Double-Checked Locking is破碎的声明”。 对于许多人来说,这是龙的存在的警钟。
隐式完整内存屏障通常包含在平台线程同步例程中,它涵盖了其核心。 然而,对于无锁编程和实现自定义的轻量级同步模式,您通常只需要屏障,甚至只需要单向屏障。
In my experience it refers to a memory barrier, which is an instruction (explicit or implicit) to synchronize memory access between multiple threads.
The problem occurs in the combination of modern agressive compilers (they have amazing freedom to reorder instructions, but usually know nothing of your threads) and modern multicore CPUs.
A good introduction to the problem is the "The 'Double-Checked Locking is Broken' Declaration". For many, it was the wake-up call that there be dragons.
Implicit full memory barriers are usually included in platform thread synchronization routines, which cover the core of it. However, for lock-free programming and implementing custom, lightweight synchronization patterns, you often need just the barrier, or even a one-way barrier only.
维基百科无所不知...
Wikipedia knows all...
内存栅栏
(内存屏障
)是一种用于同步多线程的无锁机制。 在单线程环境中重新排序是安全的。问题是排序、共享资源和缓存。 处理器或编译器能够重新排序程序指令(程序员顺序)以进行优化。 它在多线程环境中产生副作用。 这就是为什么引入内存屏障来保证程序正常运行的原因。 它速度较慢,但它解决了此类问题
[Java Happens-before]
[iOS 内存屏障]
memory fence
(memory barrier
) is a kind of lock-free mechanism for synchronisation multiple threads. In a single thread envirompment reordering is safe.The problem is ordering, shared resource and caching. Processor or compiler is able to reorder a program instruction(programmer order) for optimisation. It creates side effects in multithread envirompment. That is why
memory barrier
was introduce to guarantee that program will work properly. It is slower but it fixes this type of issue[Java Happens-before]
[iOS Memory Barriers]