redis - How to understand the atomic operation of memcache?
大家讲道理
大家讲道理 2017-05-31 10:36:15
0
1
1063

Memcache's add is atomic, that is, multiple processes are performing add operations at the same time, and no race conditions will occur. Does this mean that add will not generate concurrency? ?

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(1)
某草草

The questioner is overthinking. It is precisely because of the atomicity of add that we can be assured of concurrency.

You don’t know enough about the underlying concepts.

We say that concurrency of an operation is meaningless.

a = b + c;
Is the above statement atomic? Can the above statements be executed concurrently?

a = getValueOfB() + getValueOfC();
What about the one above?

a = add(getValueOfB(), getValueOfC());
What about this?

+ is atomic, so + cannot be concurrent? no.

We envision a world where

no operations are atomic.

Take the simplest

i++ as an example: The value of

i is 4.

At this time, 2 threads

execute i++ at the same time . (Let’s assume that simultaneous is what you call concurrency)

i++ How is it executed?

  • First get the value of i.

  • Add 1 to the value of i

  • Final results are deposited into i

If i++ is not atomic at this time, then the two threads will appear in various execution orders, thus obtaining wrong results.

  • The first thread obtained the value of i which is 4

  • The second thread obtained the value of i which is 4

  • The first thread adds 1 to i and gets 5

  • The second thread adds 1 to i, getting 5

  • The first thread stores 5 into i

  • The second thread stores 5 into i

Get the final result, i is 5. Obviously this result is wrong.

But we have not encountered this situation because i++ is atomic.

So does it mean that atomicity cannot be concurrent? Yes.

who care

From a user perspective, the

add operation must be atomic. What we are concurring with is the system, not the operation.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template