Introduction and highlights of lock-free operations

Source: Internet
Author: User

Lock-free operation highlights:
1. Applicable scenarios:
Multiple Threads simultaneously seize the same resource and perform write (modify) operations on it.

2. Several Basic concepts:
Registers: a component of a central processor. Registers are high-speed storage components with limited storage capacity. They can be used to store commands, data, and addresses. The control components of the central processor contain instruction registers (IR) and program counters (PC ). The registers contained in the arithmetic and logical components of the central processor are accumulators (ACC ).
Thread Stack: Each thread has its own exclusive thread stack, which stores the global variables and local variables and methods of this thread.
Volatile keywords: variables that are rhetorical do not rearrange computer commands, and the value of this variable is stored in registers.
CAS: Compare-and-set, which is the underlying atomic operation of a computer.

3. Common scenarios:
1) modify the configuration "Hot effective" and control the loop in another thread using one variable:
Method: declare a volatile Boolean variable. A thread can modify the value of the volatile variable by monitoring whether the configuration is modified or whether the specified rule is satisfied. The value is stored in the register, therefore, when the variable value changes, the other thread will capture the changes and respond accordingly when making condition judgments.

2). Lock-free queue:
Method: The Push/pop operation logic is as follows: declare a atomicboolean variable control. The default value is false.
While (true ){
If (control. compareandset (false, true )){
Process logic;
Control. Set (false );
Break;
}

}
The bottom-layer atomic operation is used to implement the lock-free queue, but the thread that does not obtain the lock will always judge the if condition while loop, and the CPU is 100% or higher.

3). The concept of lock-free cyclic queue solves data caching
Method: In network programming with large data volumes and high concurrency, it is particularly important to enable a large memory for data caching to improve program performance. Here we will introduce the lock removal techniques in detail:
The goal of data caching is to ensure data security (the received data cannot be overwritten before being copied). To receive and process data efficiently, the module must meet the data processing requirements (BPS for Gigabit Networks, equivalent to the I/O capability of a general SATA disk)
All the situations encountered during processing:
A) the first time the data is received, it can be stored from 0 to the cachebytes. Length of the cache array;
B). When the received data is stored in cachebytes. length, you need to perform the transpose operation. After the transpose operation, the data reception can only be stored in the last processing position of the processing pointer.
C ). when the pointer position on the receiving end and the pointer position on the processing end overlap, there are two situations: one is that the processing end is too slow to process, and the receiving end stores the pointer to catch up with the processing end; the other is that the processing end finishes processing all the data, coincidence with the acceptor pointer
The lockless solution must be able to safely handle the above three cases. A new volatile variable processbeforeread is added. The default value is true. After the acceptor is switched to true, processbeforeread is changed to true; modify processbeforeread to false after the processing end is switched. When the receiving end pointer and the processing end pointer point to the same position, the data is discarded based on the value of processbeforeread (false) or store data to cachebytes. length (true ).
Then we can see the implementation of the loop lock-free queue on Google, which is the same as the idea of this solution.

4. Conclusion:
If you want to solve the lock problem, simply list all possible conditions, and skillfully use new variables to satisfy all the conditions. In this case, it is abstracted to become a well-known XXX lock-free operation, but I forgot my idea of solving the problem...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.