What is CAs
Cas:compare and Swap, which is an atomic operation, what is atomic manipulation, can be used in multithreaded programming to achieve data exchange without interruption. is used to update variables, when multiple threads use CAS to update a variable, only one thread can update the value of the variable, the other threads fail, the failed thread is not back suspended, but is retried until it succeeds.
CAs implementation method, there are three variables, memory value V, processor cache (expected value) A, update value B. Comparing the memory value to the expected value, which means that no other thread is working on the shared data, the update memory value is B; The shared data V has already been altered by another thread, discarding the update operation, re-load the shared data, and retrying the previous operation.
Application of CAs
Optimistic Lock: Its implementation mechanism is based on CAs, each time without locking, assuming that there is no conflict to complete the operation, if there is a conflict, enrichment until the success.
Java Atom class: Atomicinteger, etc.
SOURCE Analysis
Take a look at how Atomicinteger is implemented Free-lock.
PrivateStatic Final Unsafeunsafe = Unsafe.getunsafe ();PrivateStatic finalLong Valueoffset; static {try {valueoffset = Unsafe.objectfieldoffset (AtomicInteger.class.getDeclaredField (catch (Exception ex) {throw new Error (ex);} Span class= "Hljs-keyword" >private volatile int value; public final int incrementandget (return unsafe.getandaddint (this, Valueoffset, 1) + 1;}
Value is modified with volatile, can guarantee the atomicity of get and set, but it can't guarantee the compound operation of Incrementandget read-write modification. The actual method of invocation is unsafe getandaddint
public final int getAndAddInt(Object var1, long var2, int var4) { int var5; do { var5 = this.getIntVolatile(var1, var2); } while(!this.compareAndSwapInt(var1, var2, var5, var5 + var4)); return var5; }
Unsafe some methods of invoking C + + through JNI, first obtaining the value of the values from the memory address, and then calling Compareandswapint,true: Update variable succeeded, false: unsuccessful. The most important compareandswapint after compilation is the CMPXCHG instruction, which is an atomic operation at the operating system level. So, to throw away the C + + and Assembler source code, it can be understood that Java uses the atomic OS to achieve lock-free programming.
How does the operating system guarantee the atomic nature of CAs? There will be a lock prefix before the write operation, and it will cause two things.
- Writes the data for the current processor cache row back to the memory line.
- This write cache operation invalidates the data in other CPUs that cache the memory address.
The use of bus locks guarantees atomicity:
The processor provides a lock# signal that when the processor outputs this signal on the bus, other processing requests are blocked and the processor is able to share the memory exclusively.
Cons: For multicore (32,64), where one of the CPUs locks the bus, all the other cores and memory traffic are locked and the overhead is too high.
Using cache locks to ensure atomicity
Modify the memory directly and cache the consistency mechanism to guarantee the atomicity of the operation.
ABA issues
A value is a, become B, and become a, then the use of CAS for inspection will find that its value has not changed, but actually changed, the solution is to add a version number.
Reference:
- The art of Java concurrent programming
- Solving ABA problem with atomicstampedreference
- Non-blocking synchronization algorithm and CAS (Compare and Swap) lock-free algorithm
A brief analysis of Java CAs