使用互斥锁和条件变量实现实现读写锁

只需要使用互斥锁和条件变量就能实现写者优先的读写锁。


头文件pthread_rwlock_t .h的实现:
#ifdef _MY_PTHREAD_RWLOCK_H
#define _MY_PTHREAD_RWLOCK_H


#include
#include
#include
#include


enum{ENIVAL};
typedef struct
{
pthread_mutex_t rw_mutex;//互斥量
pthread_cond_t rw_condreaders;//读锁阻塞量
pthread_cond_t rw_condwriters;//写锁阻塞量
int rw_magic;//校验量
int rw_nwaitreaders;//等待分配读锁的个数
int rw_nwaitwriters;//等待分配写锁的个数
int rw_refcount;//标志量 -1表示已分配了写锁 0表示读写锁都没有分配 正数n表示已经分配了n个读锁
}my_pthread_rwlock_t;
#define RW_MAGIC 0x19961015//校验比对值
//初始化结构体
#define MY_PTHREAD_RELOCK_INITIALIZER{
PTHREAD_MUTEX_INITIALIZER,
PTHREAD_COND_INITIALIZER,
PTHREAD_COND_INITIALIAER,
RW_MAGIC,0,0,0}
//申明读写锁解锁函数
int my_pthread_rwlock_rdlock(pthread_rwlock_t *rw);
int my_pthread_rwlock_wrlock(pthread_rwlock_t *rw);
int my_pthread_rwlock_unlock(pthread_rwlock_t *rw);
//读锁函数
int my_pthread_rwlock_rdlock(my_pthread_rwlock_t rw)
int result;
if(rw->rw_magic != RW_NAGIC)
return ENIVAL;
if((result = pthread_mutex_lock(&rw->rw_mutex)) != 0)
retrun result;

while(rw->rw_refcount < 0 || rw->rw_nwaitwriters > 0)
{
rw->rw_nwairreaders++;
result = pthread_cond_wait(&rw->rw_condreaders,&rw->rw_mutex);
rw->rw_nwaitreaders--;
if(result != 0)
break;
}
if(result == 0)
rw->rw_refcount++;
pthread_mutex_unlock(&rw->rw_mutex);
return result;
}
//写锁函数
int my_pthread_rwlock_wrlock(my_pthread_rwlock_t *rw)
{
int result;
if(rw->rw_magic != RW_MAGIC)
retrurn ENIVAL;
if(result = pthread_mutex_lock(&rw->rw_mutex)
return result;
while(rw->rw_refcount != 0)
{
rw->rw_nwaitwriters++;
result = pthread_cond_wait(&rw->rw_condwriters,&rw->rw_mutex);
rw->rw_nwaitwriters++;
if(result != 0)
break;
}
if(result == 0)
return result;
pthread_mutex_unlock(&rw->rw_mutex);
return result;
}
//解除函数


int my_pthread_rwlock_unlock(pthread_rwlock_t *rw)
{
int result;
if(rw->rw_magic != RW_MAGIC)
return ENIVAL;
if((result = pthread_mutex_lock(&rw->rw_mutex)) != 0)
return result;
if(rw->rw_refcount = -1)
rw->rw_refcount = 0;
else if(rw->rw_refcount > 0)
rw->rw_refcount--;
else
{
printf("unlock rwlock error.\n");
exit(1);
}
if(rw->rw_nwaitwriters > 0)
{
if(rw->rw_rwfcount == =)
result = pthread_cond_signal(&rw->rw_condwriters);
}
else if(rw->rw_nwaitreaders > 0)
result = pthread_cond_broadcast(&rw->rw_condreaders);
pthread_mutex_unlock(&rw->rw_mutex);
return result;
}
#endif


测试主函数test.c
#include "my_pthread_rwlock.h"
my_pthread_rwlock_t rwlock = MY_PTHREAD_RWLOCK_INITIALIZER;
void* thread_fun(void *arg)
{
my_pthread_rwlock_wrlock(&rwlock);
printf("thread fun get wrlock.\n");
printf("thread fun sleep...\n");
sleep(2);
printf("thread fun wake up.\n");
my_pthread_rwlock_unlock(&rwlock);
}
void* thread_fun1(void *arg)
{
my_pthread_rwlock_rdlock(&rwlock);
printf("thread 1 get rdlock.\n");
my_pthread_rwlock_unlock(&rwlock);
}
void* thread_fun2(void *arg)
{
my_pthread_rwlock_wrlock(&rwlock);
printf("thread 2 get wrlock.\n");
my_pthread_rwlock_unlock(&rwlock);
}
#define NUM 5
int main()
{
pthread_t tid,tid1[NUM+5],tid2[NUM];
pthread_create(&tid,NULL,thread_fun,NULL);
sleep(1);
int i;
for(i = 0;i{
pthread_create(&tid1[i],NULL,thread_fun1,NULL);
}
for(i = 0;i{
pthread_create(&tid2[i],NULL,thread_fun2,NULL);
}
pthread_join(tid,NULL);
for(i = 0;i{
pthread_join(tid1[i],NULL);
}
for(i = 0;i{
pthread_join(tid2[i],NULL);
}

return 0;
}

测试图片




 
  
 
  
 
  
 
  
 
  
 
  
 
  
 
  
 
  
 
  
 
  
 
  
 
  
 
  
 
  
 
  
 
  
 
  
 
  
 
  
 
  
 
 

你可能感兴趣的:("Linux线程间的同步“)