1. Read-write lock
Similar to the mutexes introduced earlier, but read-write locks have higher parallelism.
2. The status of the read-write lock
1. Read lock [lock in read mode]
2. Write lock [lock in write mode]
3. No lock
3. Features of read-write lock
- 1. When the read-write lock is "locked in write mode", all threads that lock the lock will be blocked before unlocking.
- 2. When the read-write lock is "locked in read mode", if the thread locks it in read mode, it will succeed; if the thread locks in write mode, it will block.
- 3. When the read-write lock is "lock in read mode", there are threads that try to lock in write mode, and there are threads that try to lock in read mode. Read-write lock
Will block subsequent read mode lock requests. Satisfy the write mode lock first. Read locks and write locks block in parallel, and write locks with high priority read-write locks are also called shared-exclusive locks. When the read-write lock is locked in read mode, it is locked in shared mode, and when it is locked in write mode, it is locked in exclusive mode. The read-write lock is very suitable for the situation that the number of reads to the data structure is much greater than the writes.
Four. Main application functions
pthread_rwlock init, function
pthread_rwlock_destroy function
pthread_rwlock_rdlock function
pthread_rwlock_wrlock, function
pthread_rwlock_tryrdlock, function
pthread_rwlock_trywrlock function
pthread_rwlock_unlock
function The return values of the above 7 functions are: success returns 0, failure directly returns an error number.
The pthread_rwlock_t type uses a sub to define a read-write lock variable.
pthread_rwlock_t rwlock;
Five. The principle of read-write lock
Six. Code Demo
#include <iostream>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
using namespace std;
#define THREAD_NUM 8
pthread_rwlock_t rwlock;
int n=0;
void*
thread_func_read(void* arg)
{
int *index = (int*)arg;
int i=4;
while(i--){
pthread_rwlock_rdlock(&rwlock);
printf("I am the %dth son thread ID:%lu \n", *index, pthread_self());
printf("the value of n is %d \n", n); // 对n读模式
pthread_rwlock_unlock(&rwlock);
usleep(100);
}
delete index;
}
void*
thread_func_write(void* arg)
{
int *index = (int*)arg;
int i=4;
while(i--){
pthread_rwlock_wrlock(&rwlock);
printf("I am the %dth son thread ID:%lu \n", *index, pthread_self());
n++; // 写模式
printf("the value of n is %d \n", n);
pthread_rwlock_unlock(&rwlock);
usleep(100);
}
delete index;
}
int
main(int argc, char*argv[])
{
pthread_t tid[THREAD_NUM];
pthread_attr_t attr;
int err, i, *value;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
pthread_rwlock_init(&rwlock, NULL);
for(i=0; i<THREAD_NUM/2; i++){
value = new int(i+1);
if( (err = pthread_create(&tid[i], &attr, thread_func_read, (void*)value)) != 0){
fprintf(stderr, "%dth thread create error the reason is %s \n", *value, strerror(err));
delete value;
}
}
for( ; i<THREAD_NUM; i++){
value = new int(i+1);
if( (err = pthread_create(&tid[i], &attr, thread_func_write, (void*)value)) != 0){
fprintf(stderr, "%dth thread create error the reason is %s \n", *value, strerror(err));
delete value;
}
}
for(i=0; i<THREAD_NUM; i++){
pthread_join(tid[i], NULL);
}
pthread_attr_destroy(&attr);
pthread_rwlock_destroy(&rwlock);
pthread_exit(NULL);
}