Reputation: 906
in multiprocessor, we know in lock inc mem
: lock can ensure no other operation can access the address mem
.
but when one processor is executing mov eax,mem
firstly, and then before it completed, second processor execute lock inc mem
.
What will be the result?
Second instruction (lock inc mem
) waits until the first (mov eax,mem
) is completed? (as a result,first will get correct value)
Both instructions execute simultaneously. (as a result, first maybe get an unpredicted value)
Upvotes: 2
Views: 161
Reputation: 92271
The mov EAX,[mem]
is atomic, in that it reads all bits in parallel. There is no chance of getting a partial result. Whether it gets the value before or after an update is not that important.
The inc [mem]
is different, in that it both reads and writes to the memory location. If you have two of them running in parallel, the result will be unpredictable as one can overwrite the result of the other. Using a lock
solves that by claiming the bus during the entire instruction. Everyone else will have to wait.
Upvotes: 2
Reputation: 58467
If mem is 32-bit aligned, the mov operation is guaranteed to be atomic. To quote volume 3A of the software developer's manual: "once started, the processor guarantees that the operation will be completed before another processor or bus agent is allowed access to the memory location"
Upvotes: 2