Linux spin Lock

Source: Internet
Author: User
Tags mutex

First, overview:

Spin lock is a kind of low-level synchronization mechanism in SMP architecture.
When thread A wants to acquire a spin lock and the lock is held by another thread lock, thread a spins in a loop to detect if the lock is already available. For an optional lock you need to note:

    • Since the spin does not release the CPU, the thread holding the spin lock should release the spin lock as soon as possible, otherwise the thread waiting for the spin lock will always spin there, wasting CPU time.
    • The thread holding the spin lock should release the spin lock before sleep so that other threads can get a spin lock.

Using any lock consumes system resources (memory resources and CPU time), and this resource consumption can be divided into two categories:

    • The resources needed to establish the lock
    • The resources required to lock the thread when it is blocked

Spin lock Lock-related APIs:

1 int Pthread_spin_destroy (pthread_spinlock_t *); 2 int int ); 3 int pthread_spin_lock (pthread_spinlock_t *); 4 int pthread_spin_trylock (pthread_spinlock_t *); 5 int pthread_spin_unlock (pthread_spinlock_t *);
1) Initialize spin lock

  The pthread_spin_init is used to request the resources required to use the spin lock and initialize it to a non-locked state. The value of pshared and its meaning:

    • Pthread_process_shared: This spin lock can be shared among threads in multiple processes.
    • Pthread_process_private: The spin lock can be used only if the thread friend in the process in which this spin lock is initialized.
2) Get a spin lock

The Pthread_spin_lock is used to obtain (lock) the specified spin lock. If the spin lock is not currently held by another thread, the thread that called the function obtains the spin lock. Otherwise the function will not return until the spin lock is obtained. If the thread that called the function has already held the spin lock when the function is called, the result is indeterminate.

3) Try to get a spin lock

Pthread_spin_trylock will attempt to get the specified spin lock, and if it cannot get it, the understanding returns failed.

4) Release (unlock) a spin lock

The Pthread_spin_unlock is used to release the specified spin lock.

5) Destroy a spin lock

Pthread_spin_destroy is used to destroy the specified spin lock and release all associated resources (so-called all refers to resources that are automatically requested by Pthread_spin_init) after calling the function if there is no call Pthread_spin_ Init initializes the spin lock, the result of any call that attempts to use the lock is undefined. If you call the

When a spin lock is being used or the spin lock is not initialized, the result is undefined.

Pthreads provides the mutex lock operation related APIs mainly:

1 pthread_mutex_lock (pthread_mutex_t *mutex); 2 pthread_mutex_trylock (pthread_mutex_t *mutex); 3 pthread_mutex_unlock (pthread_mutex_t *mutex);
The APIs provided by Pthreads with the spin lock operation are:
Pthread_spin_lock (pthread_spinlock_t *Lock * lock *Lock);

From the implementation principle, the mutex belongs to the sleep-waiting type of lock. For example, on a dual-core machine, there are two threads (thread A and thread B), which run on Core0 and Core1 respectively. Suppose that thread a wants to get a lock of a critical section through the Pthread_mutex_lock operation, and that this lock is being held by thread B, then the line

Process A will be blocked (blocking), and CORE0 will place thread A in the wait queue at this point, and Core0 can run other tasks (such as another thread C) without having to wait. Spin lock does not, however, belong to the busy-waiting type of lock if thread A is using

Pthread_spin_lock operation to request the lock, then thread A will always be busy waiting on the CORE0 and keep the lock request, until the lock is obtained

If you go to the Linux glibc in the implementation of the Pthreads API NPTL (Native POSIX Thread Library) source code (using "Getconf gnu_libpthread_version" command can get the version number of the NPTL in our system, we will find the Pthread_mutex_lock () operation if the lock is not successful, it will

Call the system Call of System_wait () and join the current thread in the mutex's wait queue. Spin lock can be understood as a lock operation implemented in a while (1) loop with inline assembly code (an impression of a paper describing that the spin lock operation in the Linux kernel requires only two CPU instructions, and the unlock operation can be done with only one instruction

). Interested friends can refer to the implementation of the Pthreds API in another microkernel named Sanos: Mutex.c SPINLOCK.C, although the implementation of the code in NPTL is not the same, but because its implementation is very simple and understandable, we understand spin The features of lock and mutex are also helpful.

For a spin lock, it only consumes a small amount of resources to establish a lock, and then when the thread is blocked, it repeatedly checks to see if the lock is available, meaning that it consumes CPU time when the spin lock is waiting.

In the case of a mutex, it consumes a lot of system resources to establish the lock compared to the spin lock, and then when the thread is blocked, the thread's dispatch state is modified, and the thread is joined to wait for the thread queue, and finally when the lock is available, the thread is taken out of the waiting queue and changed its dispatch state. But during a thread being blocked, it does not consume

CPU resources.

Therefore, spin locks and mutexes are suitable for different scenarios. Spin locks are suitable for scenes that only need to be blocked for a very short time, while mutexes are suitable for scenarios that can block for a long time.

Linux spin Lock

Related Article

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.