What is non-blocking synchronization?

Sync: When multiple threads concurrently access shared data to ensure the sharing of data at a time and then only by one or a number of threads

Blocked: that is, when read, the kernel buffers must have the data, if there is no data that has been waiting for

Non-blocking: that is, when read, the kernel buffer if there is no data, you can return immediately, errno is EGAIN, next time to read.

Examples: AtomicInteger

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;
    }

Blocking synchronization and synchronization are two means of support to achieve non-blocking thread-safe, non-blocking synchronization for synchronization, blocked mainly to solve the blocking thread synchronization blocking and performance issues caused by wake, what is called non-blocking synchronization it? In a concurrent environment, a thread of shared variables to be operated, if no other thread contention for shared data on the success of that operation; fight if there is a conflict with the data, then before going to compensatory measures, such as continuous retry mechanism, until it is successful, because this optimistic concurrency strategy does not require the thread is suspended, but also put this synchronization operation is called a non-blocking synchronization (operating and collision detection with atomic). Under the development instruction set of hardware drivers, the "operation and conflict detection" This behavior seems to require multiple operations requires only a processor instruction will be completed, including these instructions very famous CAS instruction (Compare-And -Swap compare and swap).

Simply put, if the comparison is returned if successful, it returns the result; the execution if the comparison fails, again.

 

Guess you like

Origin www.cnblogs.com/sloveling/p/11933500.html