开发者

Assumption on machine instruction in the sense of multi-thread

Can I assume that each instruction is observed atomicity? for example,

mov dword ptr [eax], 0

The movement eith开发者_开发知识库er 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.

  1. Any time when any core of processor executes an instruction(add,mov,shift, etc.), can the execution be interrupted?
  2. 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?


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


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 yes mov 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜