1, Overview
Case: write a case to test the read-write lock. Requirements: create three threads to write and five threads to read to test the read-write lock.
Basic concepts of read-write lock:
Read write locks are also called share exclusive locks. When the read-write lock is locked in read mode, it is locked in shared mode; When it is locked in write mode, it is locked in exclusive mode. Write exclusive, read shared.
Usage of read-write lock:
The number of reads to the data structure is much greater than that of writes
Characteristics of read-write lock:
1. When the read-write lock is "lock in write mode", all threads locking the lock will be blocked before unlocking
2. When the read-write lock is "lock in read mode", if the thread locks it in read mode, it will succeed; If the thread is locked in write mode, it will block.
3. When a read-write lock is "lock in read mode", there are both threads trying to lock in write mode and threads trying to lock in read mode. Then the read-write lock blocks subsequent read mode lock requests. Write mode lock is preferred. Read lock and write lock are blocked in parallel, and write lock has high priority
Read / write lock scenario exercise:
1. Thread A successfully added the write lock, and thread B requested to read the lock
A: thread B is blocked
2. Thread A holds A read lock and thread B requests A write lock
A: thread B is blocked
3. Thread A has A read lock, and thread B requests A read lock
A: thread B locks successfully
4. Thread A holds the read lock, then thread B requests the write lock, and then thread C requests the read lock
Answer: Block B, block c - Write priority high
5.A unlocks, thread B writes successfully, and thread C continues to block
Answer: B unlocks and C adds read lock successfully
6. Thread A holds the write lock, then thread B requests the read lock, and then thread C requests the write lock
Answer: BC blocking
7.A unlocks, C adds the write lock successfully, and B continues to block
Answer: C unlocks and B adds read lock successfully
Basic steps of read / write lock operation:
1. Define a lock: pthread_rwlock_t rwlock;
2. Initialize read / write lock: pthread_rwlock_init()
3. Add read lock: pthread_rwlock_rdlock()
4. Write lock: pthread_rwlock_wrlock()
5. Unlock: pthread_rwlock_unlock();
6. Release lock: pthread_rwlock_destroy();
According to the above 6 steps, let's take a look at the specific code example
2, Code example
//Read write lock test program #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <unistd.h> #include <pthread.h> //Define a global variable int number = 0; //Define a read-write lock pthread_rwlock_t rwlock; //Write thread callback function void * thread_write(void *arg){ int i = *(int *)arg; int cur; while(1){ //Write lock pthread_rwlock_wrlock(&rwlock); cur = number; cur++; number = cur; printf("[%d]-W:[%d]\n",i,cur); //Unlock pthread_rwlock_unlock(&rwlock); sleep(rand()%3); } } //Read thread callback function void *thread_read(void *arg){ int i = *(int *)arg; int cur; while(1){ //Read lock pthread_rwlock_rdlock(&rwlock); cur = number; printf("[%d]-R:[%d]\n",i,cur); //Read write lock test program #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <unistd.h> #include <pthread.h> //Define a global variable int number = 0; //Define a read-write lock pthread_rwlock_t rwlock; //Write thread callback function void * thread_write(void *arg){ int i = *(int *)arg; int cur; while(1){ //Write lock pthread_rwlock_wrlock(&rwlock); cur = number; cur++; number = cur; printf("[%d]-W:[%d]\n",i,cur); //Unlock pthread_rwlock_unlock(&rwlock); sleep(rand()%3); } } //Read thread callback function void *thread_read(void *arg){ int i = *(int *)arg; int cur; while(1){ //Read lock pthread_rwlock_rdlock(&rwlock); cur = number; printf("[%d]-R:[%d]\n",i,cur); //Unlock pthread_rwlock_unlock(&rwlock); sleep(rand()%3); } } int main(){ int n = 8; int i =0; int arr[8]; //Define thread id pthread_t thread[8]; //Initialize read / write lock pthread_rwlock_init(&rwlock,NULL); //Which write lock is created for(i=0;i<3;i++){ arr[i] = i; //Create write thread pthread_create(&thread[i],NULL,thread_write,&arr[i]); } //Create 5 read threads for(i=3;i<n;i++){ arr[i] = i; //Create read thread pthread_create(&thread[i],NULL,thread_read,&arr[i]); } //Reclaim child threads int j = 0; for(j=0;j<n;j++){ pthread_join(thread[j],NULL); } //Release lock pthread_rwlock_destroy(&rwlock); return 0; }
3, Example diagram
As shown in the following figure: the read data is always the last data written.