Linux common lock comparison-spin lock, mutual exclusion lock, semaphore, critical area __linux

Source: Internet
Author: User
Tags mutex semaphore
Spin Lock (Spinlock): The purpose of the lock is to protect shared resources and achieve thread synchronization. The spin lock distinguishes itself from other locks in that if a thread is not acquiring a lock, it will constantly ask whether the spin-lock-holding person releases the lock (the acquisition lock will spin there), constantly applying for a fetch until the spin-lock is released, the comparison applies to a situation where the lock time is relatively short (the CPU is idling). It should be noted that one lock can only be kept by one. Mutex (mutex): A thread A obtains a mutex that can access the shared resource, and when another thread B accesses the shared resource, it discovers that the mutex is already obtained by thread A, then the B thread enters the wait queue. If thread A frees a mutex, the operating system wakes up a thread that requires a mutex in the wait queue. Semaphore (semaphore): In a multithreaded environment, used to protect shared resources. The semaphore has two operations, the P operation and the V operation, a counter, and the value of the counter represents the number of resources. The P operation causes the counter to be reduced by one, which means that one thread obtains a resource and the number of resources is reduced by one. V operation is the thread to release the resource, Calculator plus one, that is, the number of resources plus one. When the counter is zero, there is no resource available for the thread to use, and no threads are waiting for the resource at this time. When another thread requests the resource, the semaphore is zero and the thread is asleep (put in the wait queue) until the wire performs the V operation. The integer semaphore cannot be negative, but the recorded semaphore can be negative, and the absolute number of negative numbers indicates the count of the threads of the waiting sequence. The resources here can be printers and so on. Critical area (critical section): A piece of code that accesses a common resource and accesses a critical resource. In simple terms, a critical section is a snippet of code that accesses a shared resource. Only one process can be allowed to enter the critical section at a time, the other processes are not allowed to enter after entry, the other processes are in the waiting state (suspended), and the process entering the critical section exits within a limited time.
In order to prevent later forget, first record down, if there is a mistake, please point out ...

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.