Read and write locks and mutual exclusion locks

Source: Internet
Author: User

There are two main types of relationships between intersecting processes, synchronous and mutually exclusive.
The so-called mutual exclusion, refers to the walk between different processes of a number of program fragments, when a process runs one of the program fragments, other processes can not run any of their program fragments, can only wait until the process runs out of this program fragment to run.
The so-called synchronization, refers to the walk in a number of different processes between the pieces of the program, their operation must be in strict accordance with the specified sequence of operation, this order depends on the specific task to be completed.  Obviously, synchronization is a more complex mutex, and mutual exclusion is a special kind of synchronization. That is, mutual exclusion is two threads can not run at the same time, they will repel each other, must wait for one thread to run, another to run, and synchronization can not run concurrently, but he must be in some order to run the corresponding thread (also a mutex)!
Summary: Mutual exclusion: Refers to a resource that allows only one visitor to access it, with uniqueness and exclusion.  However, mutual exclusion cannot limit the order in which visitors access resources, that is, access is unordered. Synchronization: Refers to the mutual exclusion of the basis (in most cases), through other mechanisms to achieve the visitor's orderly access to resources. In most cases, synchronization has been mutually exclusive, especially if all writes to the resource must be mutually exclusive. In rare cases, multiple visitors can be allowed to access resources at the same time.

Read/write Lock features:

1) Multiple readers can read at the same time

2) The writer must be mutually exclusive (only one writer is allowed to write, nor can the reader and the writer simultaneously)

3) The writer takes precedence over the reader (once the writer is written, the subsequent reader must wait, and the writer is given priority when waking)

Mutual exclusion Lock Features:

Only one thread at a time has a mutex, and the other thread waits only

Mutual exclusion Lock

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 ()

condition Variable (Condtion Variable) is a common method used in multithreaded programs to implement the "wait-and-wake" logic.

For a simple example, application A contains two threads T1 and t2. T1 needs to continue execution when the bool variable Test_cond to True, and the value of Test_cond is changed by T2, in which case how do you write the program? There are two options to choose from:

The first is T1 timed to poll the variable Test_cond, and if Test_cond is false, it continues to hibernate, and if Test_cond is true, execution begins. The second is the above mentioned condition variable, T1 call cond_wait to wait when Test_cond is false, t2 after changing the value of Test_cond, call Cond_signal, wake up in wait T1, tell T1 test_cond value changed, In this way, the T1 can continue to execute downwards.

Obviously, the second scenario in the above two scenarios is relatively superior. In the first scenario, at each polling time, if the T1 sleep is shorter, it can cause the CPU to be wasted, and if the T1 sleeps longer, the application logic is not processed in time, resulting in poor application performance. The second solution is to solve the disadvantages of polling. However, the condition variable in the process of use, more error-prone, how to use the incorrect words, it will backfire, next, I will analyze in detail how to use the condition variable, I hope to be able to use the condition variable in the process of encountering problems of friends to help.
Before starting the introduction, it is necessary to explain, in the following introduction, the need to use the mutex and condition variables related to the content, where I use the Linux pthread_mutex_t as the mutex type, pthread_cond_t for the condition variable type to introduce, For Pthread not familiar with friends, you can refer to Linux under the manual.
1. The following is the procedure for translating the example that was just started:

pthread_mutex_t Mutex;///< mutual exclusion lockpthread_cond_t cond;///< condition variables BOOLTest_cond =false; ///TODO initializing mutexes and cond///Thread 1:Pthread_mutex_lock (&mutex);///< 1  while(!Test_cond) {pthread_cond_wait (&cond, &mutex);///< 2,3 } pthread_mutex_unlock (&mutex);///< 4 Runthread1func (); ///Thread 2:Pthread_mutex_lock (&mutex);///< 5Test_cond =true; Pthread_cond_signal (&cond); Pthread_mutex_unlock (&mutex);///< 6  ///TODO destroys mutexes and cond

       through the example above, Let me introduce some of the things that you need to be aware of in the process of using conditional variables (which is also more error-prone):
        (1) The most critical point in the use of conditional variables is the use of mutexes. Careful friends should find out, I in the above example won the 1, 2, 3, 4, 5, 6 labels. Here 1, 4, 5, 6 are normal lock/unlock,2, 3 is need special explanation. 2 is entered after the pthread_cond_wait, pthread_cond_wait tune Pthread_mutex_unlock, the purpose is to ensure that after the Thread1 blocking wait, Thread2 acquires the same lock mutex when it is acquired (i.e. 5,6). 3 is thread1 after being awakened, to exit pthead_cond_wait before the pthread_mutex_lock of pthread_cond_wait, this is done in order to take control of the mutex back to the call Pthread_cond_ The thread of wait (that is, thread1). Tidy up the basic timing:

1 Lock 1 1 unlock (in 2lock221  Lock(in1 unlock

(2) in the process of using a condition variable, it is usually added with the value of a bool or int test_cond. One thing to note here is that it is important to change the test_cond before signal to ensure that the wait thread is woken up to be able to get the correct test_cond value, otherwise the consequences will be unpredictable.

Read and write locks and mutual exclusion locks

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.