spin lock

1. The concept of spin lock is
first of all a kind of lock, similar to mutual exclusion lock, whose basic function is to synchronize between threads (processes). Different from ordinary locks, after a thread A acquires an ordinary lock, if another thread B tries to acquire the lock, then this thread B will be suspended (blocked). When the cost of thread context switching caused by the processor blocking a thread is higher than the cost of waiting for resources (the lock holder keeps the lock for a relatively short time), then thread B can not give up the CPU time slice, but in the "original time slice" Wait until the lock holder releases the lock. This is the principle of spin locks. It can be seen that spin locks are a non-blocking lock.
2. Possible problems caused by spin locks:
1. Excessive occupation of CPU time : If the current holder of the lock does not release the lock for a long time, the waiter will occupy the CPU time slice for a long time, resulting in a waste of CPU resources. Therefore, you can set a time, when the lock holder does not release the lock beyond this time, the waiter will give up the CPU time slice blocking;
2. Deadlock problem : imagine that a thread tries to acquire a spin lock twice in a row ( For example, in a recursive program), the first time the thread acquires the lock, when it tries to lock for the second time, it is detected that the lock is occupied (in fact, it is occupied by itself), then at this time, the thread will keep waiting for itself Release the lock without continuing execution, causing a deadlock. Therefore, the use of spinlocks by recursive programs should follow the following principles: A recursive program must never call itself while holding a spinlock, nor should it attempt to acquire the same spinlock while recursively calling.

3. Implementation of a spin lock in JAVA:

  1. class SpinLock {  
  2.         //Atomic (CAS) operation in java  
  3.     AtomicReference<Thread> owner =  new  AtomicReference<Thread>(); //Thread object holding spin lock  
  4.     privateint count;   
  5.     publicvoid lock() {   
  6.         Thread cur = Thread.currentThread();  
  7.         //The lock function sets the owner to the current thread, and predicts that the original value is null. The unlock function sets the owner to null, and the predicted value is the current thread. When a second thread calls the lock operation, the owner value is not empty, resulting in a loop    
  8.   
  9.             //It has been executed until the first thread calls the unlock function to set the owner to null, and the second thread can enter the critical section.  
  10.         while (!owner.compareAndSet(null, cur)){  
  11.         }  
  12.     }  
  13.     publicvoid unLock() {   
  14.         Thread cur = Thread.currentThread();  
  15.             owner.compareAndSet(cur, null);  
  16.         }  
  17.     }  
  18. }  
  19. publicclass Test implements Runnable {   
  20.     staticint sum;   
  21.     private SpinLock lock;  
  22.       
  23.     public Test(SpinLock lock) {  
  24.         this.lock = lock;  
  25.     }  
  26.     public static void main(String[] args) throws InterruptedException {  
  27.         SpinLock lock = new SpinLock();  
  28.         for (int i = 0; i < 100; i++) {  
  29.             Test test = new Test(lock);  
  30.             Thread t = new Thread(test);  
  31.             t.start();  
  32.         }  
  33.           
  34.         Thread.currentThread().sleep(1000);  
  35.         System.out.println(sum);  
  36.     }  
  37.       
  38.     @Override  
  39.     public void run() {  
  40.         this.lock.lock();  
  41.         sum++;  
  42.         this.lock.unLock();  
  43.     }  
  44. }

四、java CAS

CAS是一种系统原语,是Compare And Set的缩写。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326058255&siteId=291194637
Recommended