Summary of Characteristics of Mutexes, Read-Write Locks, Spin Locks, and Condition Variables

Read-write lock features:

 

1 ) Multiple readers can read at the same time

2 ) Writers must be mutually exclusive (only one writer is allowed to write, and neither readers and writers can do it at the same time)

3 ) Writers take precedence over readers (once there are writers, subsequent readers must wait, and writers are given priority when waking up)

 

Mutex lock features:

Only one thread can own the mutex at a time, other threads can only wait

Mutual exclusion locks actively give up the CPU and enter the sleep state in the case of failure to grab the lock until the state of the lock changes and then wake up, and the operating system is responsible for thread scheduling. In order to wake up the blocked thread or process when the state of the lock changes, it is necessary to The lock is managed by the operating system, so the mutex involves context switching during the locking operation. The actual efficiency of the mutex is still acceptable. The locking time is about 100ns. In fact, a possible implementation of the mutex is to spin for a period of time, and then spin it after the spin time exceeds the threshold. Puts the thread to sleep, so using a mutex (holding the lock for a short time each time) in concurrent operations may be as effective as using a spinlock.

 

Characteristics of condition variables:

 

An obvious disadvantage of a mutex is that it has only two states: locked and unlocked. Condition variables make up for the lack of mutexes by allowing a thread to block and wait for another thread to send a signal. It is often used in conjunction with mutexes to avoid race conditions. When the condition is not met, the thread often unlocks the corresponding mutex and blocks the thread and waits for the condition to change. Once some other thread changes the condition variable, it will notify the corresponding condition variable to wake up one or more threads that are blocked by this condition variable. In general, a mutex is a mechanism for mutual exclusion between threads, and a condition variable is a synchronization mechanism.

 

Features of spin locks:

 

If the incoming thread cannot acquire the lock, the incoming thread will not give up the CPU time slice immediately, but will keep trying to acquire the lock in a loop until it is acquired. If other threads hold the lock for a long time, then spin is wasting the CPU to do useless work, but spin locks are generally used in scenarios where the locking time is very short, and the efficiency is relatively high at this time.

 

 

Mutex

pthread_mutex_init()

pthread_mutex_lock()

pthread_mutex_unlock()

 

Read-write lock

 

pthread_rwlock_init()

pthread_rwlock_rdlock()

pthread_rwlock_wrlock()

pthread_rwlock_unlock()

 

condition variable

 

pthread_cond_init()

pthread_cond_wait()

pthread_cond_signal()

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326660294&siteId=291194637