为什么在单个CPU内核上没有发生指令重新排序问题? [英] Why doesn't the instruction reorder issue occur on a single CPU core?

查看:128
本文介绍了为什么在单个CPU内核上没有发生指令重新排序问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从此帖子:

在单个CPU内核上计时的两个线程不会遇到重新排序的问题.单个内核始终知道其自身的重新排序,并将正确解析其自身的所有内存访问.但是,多核在这方面是独立运行的,因此不会真正知道彼此的重新排序.

Two threads being timesliced on a single CPU core won't run into a reordering problem. A single core always knows about its own reordering and will properly resolve all its own memory accesses. Multiple cores however operate independently in this regard and thus won't really know about each other's reordering.

为什么在单个CPU内核上不会发生指令重新排序问题?本文不做解释.

Why can't the instruction reorder issue occur on a single CPU core? This article doesn't explain it.

示例:
以下图片摘自法案中涉及的内存重新排序:

EXAMPLE:
The following pictures are picked from Memory Reordering Caught in the Act:

以下记录:

我认为记录的指令也会在单个CPU上引起问题,因为r1r2都不是1.

I think the recorded instructions can also cause issue on a single CPU, because both r1 and r2 aren't 1.

推荐答案

单个核心总是知道其自身的重新排序,并将正确解析其自身的所有内存访问.

A single core always knows about its own reordering and will properly resolve all its own memory accesses.

单个CPU内核进行重新排序,但是它知道这是自己的重新排序,并且可以做一些巧妙的技巧来假装不是.因此,事情进展得更快,而且没有怪异的副作用.

A single CPU core does reorder, but it knows it's own reordering, and can do clever tricks to pretend it's not. Thus, things go faster, without weird side effects.

然而,多个内核在这方面独立运行,因此不会真正知道彼此的重新排序.

Multiple cores however operate independently in this regard and thus won't really know about each other's reordering.

当一个CPU重新排序时,其他CPU无法对此进行补偿.想象一下,如果CPU#1正在等待对变量A的写入,那么它将从变量B中读取数据.如果CPU#2写入变量B,则如代码所示,变量A不会发生任何问题.如果CPU#2重新排序为首先写入变量A,则CPU#1不知道并尝试从变量B中读取值.这可能会导致崩溃或任何随机"行为. (英特尔芯片拥有更多的魔力使之无法实现)

When a CPU reorders, the other CPUs can't compensate for this. Imagine if CPU #1 is waiting for a write to variableA, then it reads from variableB. If CPU#2 wrotes to variableB, then variableA like the code says, no problems occur. If CPU#2 reorders to write to variableA first, then CPU#1 doesn't know and tries to read from variableB before it has a value. This can cause crashes or any "random" behavior. (Intel chips have more magic that makes this not happen)

在单个CPU内核上分时分配的两个线程不会遇到重新排序的问题.

Two threads being timesliced on a single CPU core won't run into a reordering problem.

如果两个线程都在同一个CPU上,则写的顺序无关紧要,因为如果对它们进行了重新排序,则它们都在进行中,并且只有在两个线程都在运行时,CPU才会真正切换被写入,在这种情况下,可以安全地从另一个线程读取它们.

If both threads are on the same CPU, then it doesn't matter which order the writes happen in, because if they're reordered, then they're both in progress, and the CPU won't really switch until both are written, in which case they're safe to read from the other thread.

示例

要使代码在单个内核上有问题,就必须重新排列来自进程1 的两条指令,并被进程2中断并在两条指令之间执行.但是,如果它们之间被打断,它就会知道必须重新启动它们,并且知道它处于危险状态,因此它必须中止它们两个.因此,它要么按顺序执行它们,要么在切换到进程2之前都做,或者在切换到进程2之前都不做.所有这些都避免了重新排序的问题.

For the code to have a problem on a single core, it would have to rearrange the two instructions from process 1 and be interrupted by process 2 and execute that between the two instructions. But if interrupted between them, it knows it has to abort both of them since it knows about it's own reordering, and knows it's in a dangerous state. So it will either do them in order, or do both before switching to process 2, or do neither before switching to process 2. All of which avoid the reordering problem.

这篇关于为什么在单个CPU内核上没有发生指令重新排序问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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