读写锁
概念
读写锁与互斥量类似,不过读写锁有更高的并行性。互斥量要么加锁要么不加锁,而且同一时刻只允许一个线程对其加锁。对于一个变量的读取,完全可以让多个线程同时进行操作。
pthread_rwlock_t nwlock
读写锁有三种状态,读模式下加锁,写模式下加锁,不加锁。一次只有一个线程可以占有写模式下的读写锁,但是多个线程可以同时占有读模式的读写锁。
读写锁在写加锁状态时,在它被解锁之前,所有试图对这个锁加锁的线程都会阻塞。读写锁在读加锁状态时,所有试图以读模式对其加锁的线程都会获得访问权,但是如果线程希望以写模式对其加锁,它必须阻塞直到所有的线程释放锁。
当读写锁一读模式加锁时,如果有线程试图以写模式对其加锁,那么读写锁会阻塞随后的读模式锁请求。 这样可以避免读锁长期占用,而写锁达不到请求。
读写锁非常适合对数据结构读次数大于写次敌的程序,当它以读模式锁住时,是以共享的方式锁住的;当它以写模式谈住时,是以独占的模式谈住的。
读写锁的初始化和销毁
读写锁在使用之前必须初始化
int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock, const pthread_rwlockattr_t *restrict attz);
使用完需要销毁
int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);
成功返回0﹐共败返回错误码
代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
//全局变量 锁 / 值
pthread_rwlock_t rwlock;
int i = 1;
//线程1
void *thread_func1(void *arg){
pthread_rwlock_rdlock(&rwlock); //读锁 1
//pthread_rwlock_wrlock(&rwlock); //写锁 2
printf("thread 1 : i = %d \n", i);
sleep(5);
printf("thread 1 over \n");
pthread_rwlock_unlock(&rwlock);
return (void *)1;
}
//线程2
void *thread_func2(void *arg){
pthread_rwlock_rdlock(&rwlock); //读锁 1
//pthread_rwlock_wrlock(&rwlock); //写锁 2
printf("thread 2 : i = %d \n", i);
sleep(5);
printf("thread 2 over \n");
pthread_rwlock_unlock(&rwlock);
return (void *)2;
}
int main(){
pthread_t tid1, tid2;
int err;
// 初始化读写锁
err = pthread_rwlock_init(&rwlock, NULL);
if(!err){
printf("init thread_rwlock failed \n");
return ;
}
//创建新线程1,失败直接退出
err = pthread_create(&tid1, NULL, thread_func1, NULL);
if(err){
printf("new thread 1 create failed \n");
return ;
}
//创建新线程2,失败直接退出
err = pthread_create(&tid2, NULL, thread_func2, NULL);
if(err){
printf("new thread 2 create failed \n");
return ;
}
//等待线程1,2运行完再结束
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
//销毁锁
pthread_rwlock_destroy(&rwlock);
return 0;
}
运行结果
上述代码直接运行结果
运行结果1:
thread 1 : i = 1
thread 2 : i = 1
thread 2 over
thread 1 over
若将 1 注释, 2 去掉注释
运行结果2:
thread 1 : i = 1
thread 1 over
thread 2 : i = 1
thread 2 over
读写锁一起使用代码
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
pthread_rwlock_t rwlock;
int num = 0;
void *func1(void *arg){
while(1){
if(num%2 == 0){
pthread_rwlock_wrlock(&rwlock);
num ++;
printf("thread 1 write: num = %d \n", num);
pthread_rwlock_unlock(&rwlock);
printf("\nthread 1 write over \n\n");
sleep(2);
}else{
pthread_rwlock_rdlock(&rwlock);
printf("thread 1 read : num = %d \n", num);
pthread_rwlock_unlock(&rwlock);
printf("thread 1 read over \n");
sleep(2);
}
if(num > 10) return ;
}
}
void *func2(void *arg){
while(1){
if(num%2 == 1){
pthread_rwlock_wrlock(&rwlock);
num ++;
printf("thread 2 write : num = %d \n", num);
pthread_rwlock_unlock(&rwlock);
printf("\nthread 2 write over \n\n");
sleep(2);
}else{
pthread_rwlock_rdlock(&rwlock);
printf("thread 2 read : num is %d \n", num);
pthread_rwlock_unlock(&rwlock);
printf("thread 2 read over \n");
sleep(2);
}
if(num > 10) return ;
}
}
int main(){
pthread_t tid1, tid2;
int err;
err = pthread_rwlock_init(&rwlock, NULL);
if(err){
printf("rwlock init failed \n");
}
err = pthread_create(&tid1, NULL, func1, NULL);
if(err){
printf("thread 1 create failed \n");
return ;
}
err = pthread_create(&tid2, NULL, func2, NULL);
if(err){
printf("thread 2 create failed \n");
return ;
}
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
return 0;
}
读写锁两个奇偶数
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
pthread_rwlock_t rwlock;
int num = 0;
void *func1(void *arg){
while(1){
if(num%2 == 0){
pthread_rwlock_wrlock(&rwlock);
num ++;
printf("thread 1 : num = %d \n", num);
pthread_rwlock_unlock(&rwlock);
sleep(2);
}
if(num > 10) return ;
}
}
void *func2(void *arg){
while(1){
if(num%2 == 1){
pthread_rwlock_wrlock(&rwlock);
num ++;
printf("thread 2 : num = %d \n", num);
pthread_rwlock_unlock(&rwlock);
sleep(2);
}
if(num > 10) return ;
}
}
int main(){
pthread_t tid1, tid2;
int err;
err = pthread_rwlock_init(&rwlock, NULL);
if(err){
printf("rwlock init failed \n");
}
err = pthread_rwlock_init(&rwlock, NULL);
if(err){
printf("rwlock init failed \n");
}
err = pthread_create(&tid1, NULL, func1, NULL);
if(err){
printf("thread 1 create failed \n");
return ;
}
err = pthread_create(&tid2, NULL, func2, NULL);
if(err){
printf("thread 2 create failed \n");
return ;
}
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
return 0;
}
运行结果
thread 1 : num = 1
thread 2 : num = 2
thread 1 : num = 3
thread 2 : num = 4
thread 1 : num = 5
thread 2 : num = 6
thread 1 : num = 7
thread 2 : num = 8
thread 1 : num = 9
thread 2 : num = 10
thread 1 : num = 11