Pthread spinlock spin lock

The lock mechanism (lock) is the most commonly used synchronization mechanism in multithreaded programming, which is used to protect the critical section shared among multiple threads.

Pthreads provides a variety of lock mechanisms, the common ones are:
1) Mutex (mutex): pthread_mutex_***
2) Spin lock (spin lock): pthread_spin_***
3) Condition Variable (condition variable): pthread_con_* **
4) Read/Write lock: pthread_rwlock_***

In multi-threaded programming, according to different applications, choosing the appropriate lock for synchronization has a great impact on the performance of multi-threaded programs. This article mainly compares the two locking mechanisms, pthread_mutex and pthread_spinlock, and discusses their applicable occasions .

1 Pthread mutex

Mutex is a sleep-waiting type of lock. Starting from the 2.6.x series of stable kernels, Linux mutex is a futex (Fast-Usermode-muTEX) lock
. Basic tool for locking and building high-level abstraction locks such as semaphores and POSIX mutexes. They first appeared in version 2.5.7 of kernel development; their semantics were fixed in 2.5.40 and then in the 2.6.x series of stable kernels.
Futex was created by Hubertus Franke (IBM Thomas J. Watson Research Center), Matthew Kirkwood, Ingo Molnar (Red Hat), and Rusty Russell (IBM Linux Technology Center), among others.
Futex is composed of an aligned integer variable in user space and a waiting queue in kernel space attached to it. In most cases, multi-process or multi-thread operations operate on integer variables located in futex in user space (assembly language calls The atomic operation instructions provided by the CPU to increase or decrease), and in other cases, it is necessary to operate the waiting queue located in the kernel space through a costly system call (such as waking up the waiting process/thread, or calling the current process/thread). Threads are placed in the waiting queue. Except for a few cases where multiple threads compete for locks at the same time, futex-based lock operations do not require expensive system call operations.
The
core idea of ​​this mechanism is to Non-simultaneous contention lock operations are performed in user space instead of expensive kernel system calls, thus improving efficiency.

Pthreads提供的Mutex锁操作相关的API主要有:
1、 pthread_mutex_lock (pthread_mutex_t *mutex);
2、 pthread_mutex_trylock (pthread_mutex_t *mutex);
3、 pthread_mutex_unlock (pthread_mutex_t *mutex);

Because the source code is relatively long, there is no excerpt here, you can refer to:
glibc-2.12.2/nptl/pthread_mutex_lock.c

2 Pthread spinlock

A spinlock, also known as a spin lock, is a busy-waiting type of lock. In a multiprocessor environment, a spin lock can only be held by at most one executable thread. If an executable thread attempts to acquire a contended (already held) spinlock, the thread will keep busy-waiting, spinning, or idling, waiting for the lock to become available again. If the lock is not contested, the thread of execution that requested the lock gets it immediately and continues execution.

A contended spinlock causes the thread that requested it to spin while waiting for the lock to become available again, which wastes CPU time in particular, so spinlocks should not be held for long periods of time. In fact, this is the original intention of spin locks, lightweight locking in a short period of time.

Spinlocks in the kernel cannot be used in environments that can cause sleep . For example, a thread A acquires the spin lock L; at this time, an interrupt occurs, and in the corresponding interrupt handler function B, it also tries to acquire the spin lock L, and the interrupt handler will spin. However, the original lock holder only uses the opportunity to release the spin lock after the interrupt handler ends, resulting in a deadlock.
Since multiple processors are involved, the efficiency of spin locks is very important. Because of the process of waiting for the spin lock, the processor just keeps looping and checking, and does not execute other instructions. But even so, in general, spinlock has much less overhead than process scheduling (context switch). This is why spin locks are widely used in multiprocessor environments

The APIs related to Spin Lock lock operations provided by Pthreads mainly include:
pthread_spin _lock (pthread_spinlock_t *lock);
pthread_spin _trylock (pthread_spinlock_t *lock);
pthread_spin _unlock (pthread_spinlock_t *lock);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324973936&siteId=291194637