Linux multi-thread synchronization mechanism: mutex, semaphore, condition variable

Mutex : Mutex provides protected access to shared resources. It has two states: lock and unlock, which are used to ensure that only one thread uses shared resources for a certain period of time. The data type of mutex is pthread_mutex_t

Mainly involved functions: pthread_mutex_lock() pthread_mutex_trylock() pthread_mutex_unlock()

             Pthreaf_mutex_init()  pthread_mutex_destroy()

The area locked between lock and unlock is a critical area ( if only the lock is not unlocked, the program will block and wait )

Semaphore : A semaphore is a special integer value, which is mainly used to control the mutually exclusive access of multiple threads (processes) to critical resources. The thread judges whether there is a resource to access according to the semaphore. , a semaphore is not the same as a signal.

    A semaphore is a counter that can be used to synchronize access to a shared data object by multiple threads. In order to obtain a shared resource, a thread needs to perform the following operations:

1. Test the semaphore that controls the resource

2. If the value of this semaphore is positive, the thread can use the resource, and the thread decrements the semaphore value by 1, indicating that it uses a resource unit

3. If the value of this semaphore is 0, the thread goes to sleep until the semaphore value is greater than 0. When the thread is woken up, it returns to step 1.

Condition variables : Using only mutexes is likely to cause deadlocks. Condition variables are introduced for this purpose. Condition variables allow threads to block and wait for signals sent by another thread. Using condition variables can block threads atomically until a certain As long as the conditions are met, busy waiting can be avoided.

Condition variables are often used together with mutex locks. Mutexes are mainly used to ensure the mutual exclusion of the critical section, while condition variables are used for thread blocking and waiting. After the mutex lock enters the critical section, if the conditions are not satisfied, the thread Then it turns into a waiting state, and it is woken up and executed after waiting for the condition to be satisfied, otherwise it continues to execute, and unlocks after execution.

The data type of the condition variable is pthread_cond_t

Mainly involved functions: pthread_cond_init() pthread_cond_signal() pthread_cond_wait()

              Pthread_cond_timewait()    pthread_cond_broadcast()

The role of the Pthread_cond_wait() function:

1. Unlock the incoming mutex first (automatic release means unlocking)

    2. Then wait to block and wait (the block must be locked before being executed after being awakened by pthread_cond_singal() of other threads)

 Read- write lock rwlock :

Divided into: rdlock ( read lock ) : As long as no thread holds a given read-write lock for writing, any number of threads can hold the read-write lock for reading, and share the lock

      wrlock ( write lock ) : only when no thread holds a given read-write lock for reading or writing, the read-write lock can be allocated for writing, exclusive lock

pthread_rwlock_trwlock=PTHREAD_RWLOCK_INITIALIZER;( initialize read-write lock )

pthread_rwlock_rdlock(&rwlock): allocate read lock

pthread_rwlock_wrlock(&rwlock): allocate write lock

pthread_rwlock_unlock(&rwlock): Unlock (only read and write locks)

 

After the write lock is unlocked, multiple threads allocate the write lock first when allocating the read lock and the write lock under the same conditions (the allocation of the write lock is given priority, but this is not necessary (different systems))

Guess you like

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