Optimistic lock and pessimistic lock

Source: Internet
Author: User
Recently, Xiaojing gave skynet a pr. We have discussed this for a long time. It is said that it was because the message dispatching Department of skynet was rewritten to solve another problem, which can improve the CPU usage in some cases. Previously, skynet used the cas-based lock-free structure for message scheduling. However, in essence, the data structure of concurrent queues, whether it is

Recently, Xiaojing gave skynet a pr. We have discussed this for a long time. It is said that it was because the message dispatching Department of skynet was rewritten to solve another problem, which can improve the CPU usage in some cases. Previously, skynet used the cas-based lock-free structure for message scheduling. However, in essence, the data structure of concurrent queues, whether it is

Recently, Xiaojing gave skynet a pr.

We have discussed this for a long time. It is said that it was because the message dispatching Department of skynet was rewritten to solve another problem, which can improve the CPU usage in some cases.

Previously, skynet used the cas-based lock-free structure for message scheduling. However, in essence, the data structure of the concurrent queue, whether it is the spin-lock or cas lock-free structure, in order to ensure the time sequence, the inbound or outbound queue must be in sequence, that is, multiple cores do not help improve the queue performance.

The use of a lock-free structure is nothing more than an optimistic attitude towards the occurrence of a conflict. In most cases, the conflict will not occur, and a new strategy will be adopted once it occurs.

The use of spin lock is to adopt a pessimistic strategy for conflicts, and think that conflicts often occur. Therefore, when operating shared fields, lock the exclusive resource operation.

In the end, you must wait for the previous task to finish before proceeding to the next task.

The non-Lock Structure of the program logic is often complex, so what are its advantages?

In an optimistic situation, the lock-free structure allows the processing process to be parallel as much as possible. The system's atomic commands can be used to lock a long memory write at the moment of a possible conflict, and then immediately release the lock. Locks and locks are atomic, so that the deadlock can be avoided. At the same time, there will not be too many commands locked by spin lock (if the number of threads exceeds the number of cores, thread suspension may occur during the lock process), leading to other threads waiting too long.

In skynet's core message queue scheduling module, the benefits of the lock-free structure are actually very limited. To simplify the code, we do not even need to lock the two ends of the queue separately. Because the message queue in skynet is divided into two levels. During running, if a message exists in a queue at this level, it does not enter the global queue column (the queue in which the lock is used to ensure the order ).

Since no lock-free queues are used, the data structure can be greatly simplified. You can use a simple one-way linked list to manage this queue. Previously, in order to solve the queue length problem, we have designed the secondary queue into an intrusive linked list. This time, we only need to remove the array.

After the modification, an obvious advantage is that a large number of sudden service starts (often due to the influx of transient connections resulting in agent Startup requirements. This answers a previous question.

Original article address: optimistic and pessimistic locks. Thank you for sharing them.

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.