【Linux系统】读写锁
读者写者问题
重点
读者写者问题是并发编程中的经典问题,主要研究多个进程或线程对共享数据进行读和写操作时如何实现同步和互斥,以保证数据的一致性和操作的正确性 。
- 问题核心要点
- 同步与互斥:需要确保多个读者可以同时读共享数据(读操作可并发),但写操作必须是独占的,即当有写者在写时,其他读者和写者都不能访问共享数据 。
- 避免饥饿:要合理设计同步机制,防止写者或读者因长时间等待而得不到访问共享数据的机会,即避免 “写者饥饿”(读者过多导致写者长时间无法写入)或 “读者饥饿”(写者频繁占用导致读者无法读取) 。
读者写者 vs 生产消费
- 操作性质:生产消费问题中,生产者生产数据放入缓冲区,消费者从缓冲区取数据,重点在于数据的生产和消耗这两种操作的协调;而读者写者问题重点在于对共享数据的读和写操作的控制 。
- 并发特性:生产消费模型里,多个生产者或多个消费者之间一般是互斥访问缓冲区(或使用同步队列等机制);读者写者问题中,读者之间可以并发读,只有写者与其他读写操作互斥 。
原理
下面是一段伪代码,帮助我们理解读者写者的逻辑
公共部分
uint32_t reader_count = 0;
lock_t count_lock;
lock_t writer_lock;
Reader
// 加锁
lock(count_lock);
if(reader_count == 0)lock(writer_lock);
++reader_count;
unlock(count_lock);// read;
//解锁
lock(count_lock);
--reader_count;
if(reader_count == 0)unlock(writer_lock);
unlock(count_lock);
Writer
lock(writer_lock);// writeunlock(writer_lock);
读写锁
在编写多线程的时候,有一种情况是十分常见的。那就是,**有些公共数据修改的机会比较少。相比较改写,它们读的机会反而高的多。**通常而言,在读的过程中,往往伴随着查找的操作,中间耗时很长。给这种代码段加锁,会极大地降低我们程序的效率。那么有没有一种方法,可以专门处理这种多读少写的情况呢?
有,那就是读写锁。
读写锁的行为
当前锁状态 | 读锁请求 | 写锁请求 |
---|---|---|
无锁 | 可以 | 可以 |
读锁 | 可以 | 阻塞 |
写锁 | 阻塞 | 阻塞 |
- 注意: 写独占,读共享,读锁优先级高
读写锁接口
设置读写优先
int pthread_rwlockattr_setkind_np(pthread_rwlockattr_t *attr, int pref);
/*
pref 共有 3 种选择
PTHREAD_RWLOCK_PREFER_READER_NP (默认设置) 读者优先,可能会导致写者饥饿情况
PTHREAD_RWLOCK_PREFER_WRITER_NP 写者优先,目前有 BUG,导致表现行为和 PTHREAD_RWLOCK_PREFER_READER_NP 一致
PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP 写者优先,但写者不能递归加锁
*/
初始化
int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock,const pthread_rwlockattr_t *restrict attr);
销毁
int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);
加锁和解锁
int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);
读写锁案例
#include <iostream>
#include <pthread.h>
#include <vector>
#include <cstdlib>
#include <ctime>// 共享资源
int shared_data = 0;
// 读写锁
pthread_rwlock_t rwlock;// 读者线程函数
void *Reader(void *arg) {//sleep(1); //读者优先,一旦读者进入&&读者很多,写者基本就很难进入了int number = *(int *)arg;while (true) {pthread_rwlock_rdlock(&rwlock); // 读者加锁std::cout << "读者-" << number << " 正在读取数据,数据是: " << shared_data << std::endl;sleep(1); // 模拟读取操作pthread_rwlock_unlock(&rwlock); // 解锁}delete (int*)arg;return NULL;
}// 写者线程函数
void *Writer(void *arg) {int number = *(int *)arg;while (true) {pthread_rwlock_wrlock(&rwlock); // 写者加锁shared_data = rand() % 100; // 修改共享数据std::cout << "写者-" << number << " 正在写入,新的数据是: " << shared_data << std::endl;sleep(2); // 模拟写入操作pthread_rwlock_unlock(&rwlock); // 解锁}delete (int*)arg;return NULL;
}int main()
{srand(time(nullptr) ^ getpid());pthread_rwlockattr_t attr;// 初始化读写锁属性对象pthread_rwlockattr_init(&attr);// 设置写者优先模式pthread_rwlockattr_setkind_np(&attr, PTHREAD_RWLOCK_PREFER_WRITER_NP);// 使用属性对象初始化读写锁pthread_rwlock_init(&rwlock, &attr);// 销毁读写锁属性对象pthread_rwlockattr_destroy(&attr);// 可以更高读写数量配比,观察现象const int reader_num = 2;const int writer_num = 2;const int total = reader_num + writer_num;pthread_t threads[total]; // 假设读者和写者数量相等// 创建读者线程for (int i = 0; i < reader_num; ++i){int *id = new int(i);pthread_create(&threads[i], NULL, Reader, id);}// 创建写者线程for (int i = reader_num; i < total; ++i){int *id = new int(i - reader_num);pthread_create(&threads[i], NULL, Writer, id);}// 等待所有线程完成for (int i = 0; i < total; ++i){pthread_join(threads[i], NULL);}pthread_rwlock_destroy(&rwlock); // 销毁读写锁return 0;
}
Makefile
reader_writer_lock_test:reader_writer_lock_test.cg++ -o $@ $^ -lpthread
clean:rm -f reader_writer_lock_test
读者优先 (Reader-Preference)
在这种策略中,系统会尽可能多地允许当多个读者同时访问资源(比如共享文件或数据库),而不会优先考虑写者。这意味着当有读者正在读取时,新到达的读者会立即被允许进入,而写者会被阻塞。当写者请求写入权限时,系统会尽快地让写者获得机会,但会导致写者饥饿(即写者长时间无法获得写入权限),特别是当读者频繁到达时。
写者优先 (Writer-Preference)
在这种策略中,系统会优先考虑写者。当写者请求写入权限时,系统会尽快地让写者进入而会阻塞此时有读者完成在读取并离开。这通常意味着一旦有写者到达,减少有后续等待的读者进入,直到写者完成写入并离开。写者优先策略可以减少写者等待的时间,但可能会导致读者饥饿(即读者长时间无法获得读取权限),特别是当写者频繁到达时。