多线程意义上的机器指令假设
我可以假设每条指令都遵守原子性吗?例如,
mov dword ptr [eax], 0
该运动要么成功移动,要么不发生。指令执行过程中没有中断。
我的假设正确吗?
我知道当前的处理器可以乱序执行指令,并且编译会在优化方面生成乱序代码。因此,该运动将按照与我编写的顺序不同的顺序执行。但这并不重要,我关心的是单条指令一旦执行完毕,就不能被中断。
编辑:
我关心的是任何单个指令的原子性,而不是特殊指令或读写指令的分支,我只是使用mov作为例子。
- 任何时候,当处理器的任何核心执行指令(add、mov、shift等)时, 执行过程可以被中断吗?
- 寄存器或内存中是否存在不确定的状态(机器字大小)。
或者说硬件能够提供原子性的最小单位是什么?
Can I assume that each instruction is observed atomicity? for example,
mov dword ptr [eax], 0
The movement either move successfully or doesn't happen. there is no interrupt in the middle of the instruction executed.
Is my assumption right?
I know the current processor can execute instruction out of order, and compiles will generate codes out of order in terms of optimization. And hence the movement will be executed in the order which isn't same as I wrote. But this doesn't matter, what i concern is once the single instruction is executed, it can't be interrupted.
EDIT:
what I concern is atomicity of any single instruction, not an special instruction or a branch of read-write instructions, I just use mov for example.
- Any time when any core of processor executes an instruction(add,mov,shift, etc.),
can the execution be interrupted? - Is there any indeterminate state in the register or the memory(machine-word-size).
Or what is the smallest unit that hardware can provide atomicity?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不。您通常不应假设指令是原子的。关于用常量加载寄存器,为什么这很重要?您是想问寄存器是否会处于不确定状态?答案是否定的,否则中断将不起作用。从在同一内核上运行的程序的角度来看,寄存器要么被加载,要么不被加载。
x86 中的 LOCK 前缀是为了确保原子性。
编辑:问题已被编辑以显示将常量存储到内存中。
而我的回答通常仍然是否定的。可能在某些情况下,如果内存对齐,并且 CPU 保证这将是原子的,但我不会依赖它,因为它可能会给你带来麻烦。
另请参阅此处:
在无锁的 x86 机器上读取/写入 int
NO. You generally should not assume that instructions are atomic. With regard to loading a register with a constant why would that matter anyways? Are you asking if the register can end up in an indeterminate state? The answer to that is no, otherwise interrupts wouldn't work. The register would either be loaded or not loaded from the view point of a program running on the same core.
The LOCK prefix in x86 is there to ensure atomicity.
EDIT: Question has been edited to show storing a constant into memory.
And my answer is still generally no. There may be some situations where if the memory is aligned, and the CPU makes that guarantee this will be atomic but I wouldn't rely on it as it could get you into trouble.
Also see here:
Read/Write an int on x86 machine without lock
您已将此问题标记为 C 和 C++,但这两种语言都没有 mov 指令或任何单独指令的概念。如果您的问题确实是关于 x86 汇编,那么 mov 指令是原子的,至少只要内存操作数对齐,即使没有对齐也可能如此。但请注意,这与 C 或 C++ 变量的赋值是否是原子的无关。
You've tagged this question C and C++, but neither language has a
mov
instruction or any notion of individual instructions. If your question is really about x86 assembly, then yesmov
instructions are atomic, at least as long as the memory operand is aligned, and probably even if it's not. Note however that this has nothing to do with whether assignment to C or C++ variables is atomic.