读写锁
读写锁的分配规则
1. 没有线程持有读写锁进行写,任意数量的线程可以持有该读写锁用于读
2. 只有没有线程持有给定的读写锁用于读或者写的时候,才能分配读写锁用于写。
如果修改数据频繁,那么可以考虑用读写锁替代互斥锁。
获取与释放
- 如果对应的读写锁已由某个写入者持有,那么阻塞pthread_rwlock_rdlock获取读出锁
- 如果对应的读写锁已由另一个写入者持有,那就阻塞pthread_rwlock_wrlock获取写入锁。
- pthread_rwlock_unlock用于释放读出锁或者写入锁。
三者成功时返回0,出错时返回正的错误值 - pthread_rwlock_tryrdlock(pthread _rwlock_t *rwptr)尝试获取读出锁,如果不能马上获得,返回EBUSY错误。
- pthread_rwlock_trywrlock(pthread _rwlock_t *rwptr)尝试获取写入锁,如果不能马上获得,返回EBUSY错误。
初始化和摧毁
- 动态初始化
int pthread_rwlock_init(pthread_rwlock_t *rwptr, const pthread_rwlockattr_t *attr)
- 静态初始化
PTHREAD_RWLOCK_INITIALIZER
- 摧毁
int pthread_rwlock_destroy(pthread_rwlock_t *rwptr)
属性设置
int pthread_rwlockattr_init(pthread_rwlockattr_t * attr)
int pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr)
int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t * attr,int *valptr)
int pthread_rwlockattr_setpshared(pthread_rwlockattr_t * attr,int val)
读写锁很适合读的次数远大于写的次数的情况。
例子: 一个写者,多个读者。用读写锁进行协调工作。
code:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct data{
int number;
char info[105];
};
struct data *pdata = NULL;
pthread_rwlock_t lock;
void *read(void *arg){
int id = *((int *)arg);
while(1){
int ret;
ret = pthread_rwlock_tryrdlock(&lock);
if(ret != 0) continue;
printf("reader %d is reading!\n",id);
if(pdata == NULL) printf("data is null.\n");
else printf("data: number is %d, info are %s.\n",pdata->number,pdata->info);
pthread_rwlock_unlock(&lock);
}
pthread_exit(0);
}
void *write(void *arg){
int id = *((int *)arg);
while(1){
int ret;
ret = pthread_rwlock_trywrlock(&lock);
if(ret !=0) continue;
printf("writer %d is writing!\n",id);
if(pdata == NULL){
pdata = (struct data *)malloc(sizeof(struct data));
pdata->number = 1;
strcpy(pdata->info,"I love linux.");
printf("finish, wrote it.\n"</