[Multithreading] CAS principle

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/wrs120/article/details/90743906

introduction

  And contract in such a package in Java, in java.util.concurrent.atomic, the packet is part of the data types to the Java atom package, on the basis of the original data type, an atomic operation provides methods to ensure the security thread . How to ensure that they are thread-safe that, using the CAS mechanisms:

CASE

  CAS refers to a special instruction modern CPU broad support for a shared data memory to operate (underlying implemented in C). This instruction will make the shared memory data read and write operations atoms. CAS exchanged compared with the pseudo-code can be expressed as:

do{   
       备份旧数据;  
       基于旧数据构造新数据;  
}while(!CAS( 内存地址,备份的旧数据,新数据 ))  

  An operation instruction process: first, the CPU copies the desired data in the memory to be compared to change values. Then, when the two values are equal, the CPU will replace the value in the memory to a new value. Otherwise it would not operate. Finally, CPU will return to the old values. This series of operations are atomic. Although they may seem complicated, but it is better than the original Java 5 concurrency locking mechanism of the party. In simple terms, CAS means "I think what the original value should be, if it is, then the original value is updated to the new value, otherwise it will not be modified, and told me how much the original value."
  Simply put, CAS has three operands, memory value V, the expected value of the old A, to modify the new value B, "I think the value of V be A, if so, then updates the value of V B otherwise, do not modify the actual value of V and tells how much ", CAS is optimistic locking technique, when multiple threads attempt to use the CAS simultaneously update the same variable, only one thread can update the values of variables, and other threads have failed, failure of thread and will not be suspended, but was told that the competition fails, and you can try again .

CAS shortcomings

The basic problem is this:

  1. A process P1 is read in a shared variable
  2. P1 is preempted, and process P2 execution
  3. P2 in the shared variable value changed from A to B, and then change back to A, while the preempted P1.
  4. P1 came back to see where the value of the shared variable is not changed, then proceed.

Although the P1 value of the variable that has not changed, continue, but this will lead to some potential problems. ABA problem most likely to occur in the lock free algorithms, bear the brunt of CAS, CAS judgment is because the pointer address . If this address is to reuse it, the problem is very big. (The address is reused is very often the case, the release of a memory allocation, redistribution, likely or the original address). As: You take a suitcase full of money at the airport, this time over a hot sexy beauty, and she was very ambiguous tease you, and take advantage of when you are not careful, to use an identical suitcases and boxes full of money that you transfer the packages, and then left, you see your suitcase still there, so he went to catch a flight carrying a suitcase to go.

Guess you like

Origin blog.csdn.net/wrs120/article/details/90743906