问题描述
我读过 x86 的 INC 指令不是原子的.我的问题是怎么来的?假设我们在 x86-64 上递增一个 64 位整数,我们可以用一条指令来完成,因为 INC 指令适用于内存变量和寄存器.那么为什么它不是原子的?
I've read that INC instruction of x86 is not atomic. My question is how come? Suppose we are incrementing a 64 bit integer on x86-64, we can do it with one instruction, since INC instruction works with both memory variables and register. So how come its not atomic?
推荐答案
为什么会这样?处理器内核仍然需要读取存储在内存位置的值,计算它的增量,然后将其存储回来.读取和存储之间存在延迟,同时另一个操作可能会影响该内存位置.
Why would it be? The processor core still needs to read the value stored at the memory location, calculate the increment of it, and then store it back. There's a latency between reading and storing, and in the mean time another operation could have affected that memory location.
即使出现乱序执行,处理器内核也足够聪明",不会被自己的指令绊倒,也不会负责在时间间隔内修改此内存.但是,另一个内核可能发出修改该位置的指令,DMA 传输可能影响了该位置,或者其他硬件以某种方式触及了该内存位置.
Even with out-of-order execution, processor cores are 'smart' enough not to trip over their own instructions and wouldn't be responsible for modifying this memory in the time gap. However, another core could have issued an instruction that modifies that location, a DMA transfer could have affected that location, or other hardware touched that memory location somehow.
这篇关于为什么x86的INC指令不是原子的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!