Atomic instruction : How can other thread update the value while Compare and Swap instruction is in progress?

116 Views Asked by At

As per my understanding, any atomic instructions(compare_and_swap, test_and_test, fetch_and_add) are executed as a single instruction. Though they involve multiple CPU cycles/operations, it is invisible to the thread/process. If a thread is executing any such atomic instruction, no other thread is allowed to modify/access any values(memory locations/registers) involved in such atomic instruction.

If that is the case, Why in wikipedia http://en.wikipedia.org/wiki/Compare-and-swap, it is stated that if the value had been updated by another thread in the meantime, the write would fail. ?

How can other thread update the value while compare_and_swap instruction in one thread is under progress ?

1

There are 1 best solutions below

0
On

It's kind of confusing wording. It's really that the write won't go through immediately. Typically the way code is written it would mean that the write would be blocked until the other thread finished. To say that the write would fail would not be truthful.