I. Read-write lock
In the multi-threaded environment, in order to prevent the violation of critical resource access we tend to add mutexes to the thread function to complete mutual exclusion between threads; however, in some cases, mutex mutexes are not so efficient, such as when reading and writing to a buffer, because the need to read more than the write modification, Reading the data does not modify the number or contents of the buffer, and if you want to use a mutex, it takes extra time, and every read grabs the lock resource pending, so you can use another lock mechanism ———— read-write lock.
The existence of a read-write lock will of course have the reader and the writer, that is, multiple threads, but the interrelationships between them are different from the mutex lock:
When the read-write lock is in write-lock mode, the other writer or reader is blocked and the lock resource is not available to access the critical resource;
When the read-write lock is in the reading lock mode, other readers can also get access to the critical resource to read the data, but the writer requests that the locking thread will be blocked and cannot be locked;
When the read-write lock is in the reading-lock mode, if a writer requests a read-write lock to write and lock, the read-write lock usually blocks the thread that then requests the read locking, avoiding the read-write lock being occupied by the reader and unable to write the data into the buffer;
-------------------------------------------------------------------------------------------
Two. read-write lock function
Creation and destruction of read-write locks
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/7F/5F/wKioL1ccdymB1wRuAAAmlueDGVY428.png "style=" float: none; "title=" Init&des.png "alt=" Wkiol1ccdymb1wruaaamluedgvy428.png "/>
function parameters,
Rwlock is a pointer of type pthread_rwlock_t read-write lock type, a variable that points to read-write lock type;
attr is a property that represents a read-write lock and is set to NULL to use the system default property;
2. Locking and unlocking of Read and write locks
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7F/62/wKiom1ccdmTw6lTHAAAIxDmlQuU893.png "style=" float: none; "title=" Wrlock.png "alt=" Wkiom1ccdmtw6lthaaaixdmlquu893.png "/>
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7F/62/wKiom1ccdmSRx_u1AAAIo0_jFc8479.png "style=" float: none; "title=" Rdlock.png "alt=" Wkiom1ccdmsrx_u1aaaio0_jfc8479.png "/>
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7F/5F/wKioL1ccd-SxzuwrAAAHn11azlg858.png "title=" Unlock.png "alt=" Wkiol1ccd-sxzuwraaahn11azlg858.png "/>
Rwlock is a pointer to a read-write lock;
The pthread_rwlock_wrlock function is to add a read-lock mode to the reader;
The pthread_rwlock_rdlock function is a write-lock mode for the writer;
A try is added to the function to indicate the non-blocking mode that the thread calls the function;
The function returned 0 successfully and failed to return the error code;
-------------------------------------------------------------------------------------------
Chestnut Time:
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/7F/60/wKioL1ccgzTyqlEGAAB5jV0XOCg124.png "title=" rw.png "alt=" Wkiol1ccgztyqlegaab5jv0xocg124.png "/>
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7F/62/wKiom1ccgtuzheXSAABS1XvZjpE949.png "style=" float: none; "title=" Main.png "alt=" Wkiom1ccgtuzhexsaabs1xvzjpe949.png "/>
The above pest created two reader threads and two writer threads, reader thread function plus read lock, writer thread function plus write lock, run program observation result:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/7F/62/wKiom1ccgtyQ358cAAAJM13DRoA384.png "style=" float: none; "title=" Ret1.png "alt=" Wkiom1ccgtyq358caaajm13droa384.png "/>
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7F/60/wKioL1ccg6LSh3e6AAAKh7pC1Pc970.png "style=" float: none; "title=" Ret2.png "alt=" Wkiol1ccg6lsh3e6aaakh7pc1pc970.png "/>
The results of each run of the program are not the same, because the read-write lock is a priority, in the above pest the priority of the higher, the first result is because the two writer thread priority to obtain a read-write lock, write data to the BUF buffer, so two readers read the same data, each reading results in the difference of 2 In the second result, the writer's priority is still high, because each time the writer writes the data, the reader reads the data, and a thread does not read the duplicate data, but when a writer thread writes the data, a reader thread obtains the read-write lock and reads the data, and the two interact;
-------------------------------------------------------------------------------------------
A read-write lock is a spin lock, unlike a mutex, where the lock resource is not available, the thread that requests the lock resource suspends waiting until a lock resource is available to be awakened, while the read-write lock is not suspended while the lock resource is occupied, and the lock resource is repeatedly requested until the lock resource is released. ;
Although the lock types are different, you can use mutexes and condition variables to simulate read and write locks:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/7F/60/wKioL1cckRjQDRueAACDR1v7WGE035.png "style=" float: none; "title=" change. png "alt=" Wkiol1cckrjqdrueaacdr1v7wge035.png "/>
The chestnut above will read and write the lock with mutex and conditional variable simulation:
First, because the writer and the writer is mutually exclusive relationship, so must add the mutex, and the reader and the reader does not need mutually exclusive relationship, the efficiency of the read and write lock is that multiple readers can pull the data from the buffer at the same time period, so the reader thread function does not need to join the mutex; To maintain the mutual exclusion between the reader and the writer, the conditional variable can be added , when the reader's condition is not satisfied, that is, when the writer writes the data to the buffer, the reader needs to suspend the wait, in order for the reader to read the data when the writer does not go to the buffer to write data, Can let the writer sleep on 1 seconds, that is, to ensure that the reader once again into wait waiting for the writer to obtain a mutex to buffer the operation , when the writer finished writing data, need to call pthread_cond_broadcast function to wake all waiting threads, that is, a reader in a read-write lock who obtains the lock resource can also have access;
To run the program:
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/7F/60/wKioL1cckRjBkMrAAAAJvto32v8775.png "style=" float: none; "title=" changed Ret.png "alt=" Wkiol1cckrjbkmraaaajvto32v8775.png "/>
-------------------------------------------------------------------------------------------
Summarize:
Mutexes can be used when a thread needs to occupy a lock longer, the other thread needs to suspend waiting, if the use of spin lock will always stay in the spin request lock resource, and if a thread takes up the lock time is not long, you can use read-write lock this spin lock, Because it saves the time it takes for the system to suspend and wake up a process, the use of the lock can be combined with different usage requirements.
Finish
This article is from the "Knock Code good Sleep zzz" blog, please be sure to keep this source http://2627lounuo.blog.51cto.com/10696599/1767275
Mutual exclusion and conditional variables simulation for read-write lock ————