Linux Kernel synchronization method

Source: Internet
Author: User

1, atomic operation, is the basis of other synchronization methods.

2, Spin lock, the thread tries to acquire a spin lock that has been held by someone else, the current thread is busy waiting, consuming CPU resources.

3, read and write spin lock, according to the characteristics of universality and specificity, ordinary spin lock in a specific scenario will degrade. Therefore, a read-write spin lock is provided, a reading lock can be read, a lock cannot be written, and a write lock cannot be added with any lock.

4, need to pay attention to several items:

Normal spin locks are not recursive. Read locks can be recursive, and write locks are not recursive.

The lock on the surface is the code, which actually locks the shared data.

When using a read-write lock, it is important to note that read locks can be read locks, that multiple threads occupy read locks, that all threads must be released to add write locks, which often causes write locks to be starved for long periods of time.

5, the problem of spin lock, the thread tries to acquire a spin lock that has been held by someone else, the current thread is busy waiting, consuming CPU resources. How to solve this problem?

Using semaphores, the semaphore is a sleep lock. A task attempts to acquire a semaphore that is possessed by someone else, and the semaphore pushes it into a waiting queue to sleep, and when the requested semaphore is released, the task in the waiting queue is awakened and the semaphore is received.

6, it should be noted that the semaphore is a sleep lock, but it will also have a cost, context switch, the blocked thread to swap out, that is, let it sleep and wake it, spending a certain amount of overhead. If the time for each thread lock is very short, a spin lock is generally used, and the busy wait time is also very short. If the lock takes a long time, the semaphore is used.

7, compared to spin lock, the signal volume has a wider use, using the PV operation can not only protect the shared resources, but also can control the number of simultaneous access, but also control the order of access. For locks, the PV operation is the one who locks who is released, while the semaphore can then be different between threads.

8, consider a special use of the signal volume of the scene, you can sleep the mutual exclusion lock. The semaphore capacity created is 1 and the usable quantity is 0, which means that the number of simultaneous accesses is 1. This is the mutex, the mutex lock can be considered v operation, and then P operation.

Linux Kernel synchronization method

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.