什么是内存栅栏?

发布于 2024-07-09 00:26:17 字数 21 浏览 9 评论 0原文

使用显式内存栅栏是什么意思?

What is meant by using an explicit memory fence?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

痴梦一场 2024-07-16 00:26:17

为了提高性能,现代 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).

停顿的约定 2024-07-16 00:26:17

我的答案复制到另一个问题,处理器有哪些技巧优化代码?

最重要的是内存访问重新排序。

如果没有内存栅栏或序列化指令,处理器可以自由地重新排序内存访问。 某些处理器架构对其可以重新排序的数量有限制; Alpha 因最弱而闻名(即,最能重新排序的)。

对这个主题的很好的处理可以在 Linux 内核源文档中找到,位于 Documentation/memory-barriers.txt

大多数时候,最好使用编译器或标准库中的锁定原语; 这些都经过了良好的测试,应该具备所有必要的内存屏障,并且可能已经过优化(优化锁定原语很棘手;即使是专家有时也会出错)。

Copying my answer to another question, What are some tricks that a processor does to optimize code?:

The most important one would be memory access reordering.

Absent memory fences or serializing instructions, the processor is free to reorder memory accesses. Some processor architectures have restrictions on how much they can reorder; Alpha is known for being the weakest (i.e., the one which can reorder the most).

A very good treatment of the subject can be found in the Linux kernel source documentation, at Documentation/memory-barriers.txt.

Most of the time, it's best to use locking primitives from your compiler or standard library; these are well tested, should have all the necessary memory barriers in place, and are probably quite optimized (optimizing locking primitives is tricky; even the experts can get them wrong sometimes).

不知在何时 2024-07-16 00:26:17

根据我的经验,它指的是内存屏障,这是一条用于同步的指令(显式或隐式)多个线程之间的内存访问。

问题出现在现代激进编译器(它们具有惊人的自由度来重新排序指令,但通常对线程一无所知)和现代多核 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.

新人笑 2024-07-16 00:26:17

维基百科无所不知...

内存屏障,也称为 membar
或记忆栅栏,是一类
导致中央的指令
处理单元(CPU)强制执行
内存的排序约束
之前和之后发布的操作
屏障指令。

CPU 采用性能优化
这可能会导致乱序
执行,包括内存加载和
商店运营。 内存操作
重新排序通常不会被注意到
在单个执行线程内,
但会导致不可预测的行为
并发程序和设备驱动程序
除非仔细控制。 最正确
排序约束的性质是
依赖于硬件,并由
架构的内存模型。 一些
架构提供了多种
执行不同的障碍
订购限制。

通常使用内存屏障
当实现低级机器时
对共享内存进行操作的代码
多个设备。 此类代码包括
同步原语和
无锁数据结构
多处理器系统和设备
与计算机通信的驱动程序
硬件。

Wikipedia knows all...

Memory barrier, also known as membar
or memory fence, is a class of
instructions which cause a central
processing unit (CPU) to enforce an
ordering constraint on memory
operations issued before and after the
barrier instruction.

CPUs employ performance optimizations
that can result in out-of-order
execution, including memory load and
store operations. Memory operation
reordering normally goes unnoticed
within a single thread of execution,
but causes unpredictable behaviour in
concurrent programs and device drivers
unless carefully controlled. The exact
nature of an ordering constraint is
hardware dependent, and defined by the
architecture's memory model. Some
architectures provide multiple
barriers for enforcing different
ordering constraints.

Memory barriers are typically used
when implementing low-level machine
code that operates on memory shared by
multiple devices. Such code includes
synchronization primitives and
lock-free data structures on
multiprocessor systems, and device
drivers that communicate with computer
hardware.

拒绝两难 2024-07-16 00:26:17

内存栅栏内存屏障)是一种用于同步多线程的无锁机制。 在单线程环境中重新排序是安全的。

问题是排序、共享资源和缓存。 处理器或编译器能够重新排序程序指令(程序员顺序)以进行优化。 它在多线程环境中产生副作用。 这就是为什么引入内存屏障来保证程序正常运行的原因。 它速度较慢,但​​它解决了此类问题

[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]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文