Linux multi-threaded synchronization mechanism: Mutex, semaphore, condition variable

Source: Internet
Author: User

Mutex Amount: The mutex provides protected access to the shared resource, two states of it: Lock and unlock, to ensure that only one thread is using the shared resource for a certain period of time, and that the data type of the mutex is pthread_mutex_t
Main 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 locking the Unlock program will block the wait)
Signal Volume: Semaphore is a special integer value, mainly used to control the multiple threads (processes) of the critical resources of mutually exclusive access, the thread according to the semaphore to determine whether there is access to resources, semaphore is a thread synchronization mechanism, the signal volume and signal is different.
A semaphore is a counter that can be used to synchronize multiple threads to access a shared data object, and in order to obtain a shared resource, the thread needs to do the following:
1. Test the semaphore that controls the resource
2, if the value of this semaphore is positive, the thread can use the resource, the thread will reduce 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 awakened, it returns to the 1th step.
condition Variable: A mutex is most likely to cause a deadlock, introducing a conditional variable that allows a thread to block and wait for a signal sent by another thread, and use a condition variable to block the thread atomically until a condition is met, to avoid being busy, and so on.
The condition variable is used together with the mutex, the mutex is mainly used to guarantee the mutual exclusion of the critical section, while the condition variable is used for the blocking wait of the thread, when the mutex lock enters the critical section, if the condition is not satisfied, the thread will turn to wait, wait for the condition to be satisfied and then wake up to execute.
The data type of the condition variable is pthread_cond_t
Main involved functions: Pthread_cond_init () pthread_cond_signal () pthread_cond_wait ()
Pthread_cond_timewait () Pthread_cond_broadcast ()
The pthread_cond_wait () function works:
1. Unlock the mutex that is passed in first (auto-release is unlocked)
2. Wait for blocking waits (blocked by another thread's pthread_cond_singal () must be locked before execution)

read/write lock Rwlock:
Divided into: rdlock (read lock): As long as no thread holds a given read-write lock for writing, then any number of threads can hold the read-write lock for reading, shared lock
Wrlock (Write lock): only if no thread holds a given read-write lock for reading or writing, the read-write lock can be allocated for write, exclusive lock
pthread_rwlock_t Rwlock=pthread_rwlock_initializer; (Initialize read-write lock)
Pthread_rwlock_rdlock (&rwlock): Assigning read locks
Pthread_rwlock_wrlock (&rwlock): Assigning write locks
Pthread_rwlock_unlock (&rwlock): Unlock (only read write lock)

When a write lock is unlocked, multiple threads allocate read and write locks in the same condition, assigning write locks (priority is given to assigning write locks, but this is not required (System differs))

Linux multi-threaded synchronization mechanism: Mutex, semaphore, condition variable

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.