Linux下的读写锁

读写锁其实是一种特殊的自旋锁

在linux下有两种锁:

                               (1)自旋锁:不断地周而复始的做一件事情,不挂起,轮询

                               (2)挂起等待锁:信号量,互斥锁

读写锁也有三种关系:             写者和写者:互斥

                                                     读者和读者:没有关系,因为读者只读,不做其他操作,所以可以说是共享关系或者说没有关系

                                                     写者和读者:互斥,同步

读写锁的相关函数:

初始化读写锁        int pthread_rwlock_init (pthread_rwlcok_t *restrict rwlock)        参数是声明的读写锁变量

删除读写锁            int pthread_rwlock_destroy (pthread_rwlcok_t *rwlock) 

加锁有两种函数    (1)阻塞式申请    int pthread_rwlock_rdlock (pthread_rwlock_t *rwlock)-----加只读锁

                                                                  int pthread_rwlock_wrlock (pthread_rwlock_t *rwlock)-----加只写锁

                                (2)非阻塞式申请  int pthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock)-----加只读锁

                                                                    int pthread_rwlock_trywrlock (pthread_rwlock_t *rwlock)-----加只写锁    

释放锁                    int pthread_rwlock_unlock(pthread_rwlock_t * rwlock)

测试代码:

  1 #include<stdio.h>
  2 #include<pthread.h>
  3 int book;
  4 pthread_rwlock_t rwlock;
  5 void* read(void* arg)
  6 {
  7//   sleep(1);//(2)
  8     while(1)
  9     {
 10         if(pthread_rwlock_tryrdlock(&rwlock)!=0)
 11         {
 12             printf("writer is writing..!\n");
 13         }else{
 14             printf("read book is:%d\n",book);
 15 //          sleep(3);//(1)
 16             pthread_rwlock_unlock(&rwlock);
 17         }
 18     }
 19 }
 20 
 21 void* write(void* arg)
 22 {
 23 //  sleep(1);//(1)
 24     while(1)
 25     {
 26         if(pthread_rwlock_trywrlock(&rwlock)!=0)
 27         {
 28             printf("reader is reading..!\n");
 29         }else{
 30             book++;
 31             printf("write done:%d\n",book);
 32//           sleep(3);//(2)
 33             pthread_rwlock_unlock(&rwlock);
 34         }
 35     }
 36 }
 37 int main()
 38 {
 39     pthread_rwlock_init(&rwlock,NULL);
 40     pthread_t reader,writer;
 41     pthread_create(&reader,NULL,read,NULL);
 42     pthread_create(&writer,NULL,write,NULL);
 43 
 44     pthread_join(reader,NULL);
 45     pthread_join(writer,NULL);
 46     pthread_rwlock_destroy(&rwlock);
 47     return 0;
 48 }

运行结果因为没有限制读写次数,现象是一瞬间读很多次,写很多次。

所以我们改变一下测试代码,观察两种情况

(1)读者可以读成功,sleep 3秒,但同时1秒后写者到达,但是写者没有写成功,因为读者在sleep,并没有释放锁

  1 #include<stdio.h>
  2 #include<pthread.h>
  3 int book;
  4 pthread_rwlock_t rwlock;
  5 void* read(void* arg)
  6 {
  7//   sleep(1);//(2)
  8     while(1)
  9     {
 10         if(pthread_rwlock_tryrdlock(&rwlock)!=0)
 11         {
 12             printf("writer is writing..!\n");
 13         }else{
 14             printf("read book is:%d\n",book);
 15             sleep(3);//(1)
 16             pthread_rwlock_unlock(&rwlock);
 17         }
 18     }
 19 }
 20 
 21 void* write(void* arg)
 22 {
 23     sleep(1);//(1)
 24     while(1)
 25     {
 26         if(pthread_rwlock_trywrlock(&rwlock)!=0)
 27         {
 28             printf("reader is reading..!\n");
 29         }else{
 30             book++;
 31             printf("write done:%d\n",book);
 32//           sleep(3);//(2)
 33             pthread_rwlock_unlock(&rwlock);
 34         }
 35     }
 36 }
 37 int main()
 38 {
 39     pthread_rwlock_init(&rwlock,NULL);
 40     pthread_t reader,writer;
 41     pthread_create(&reader,NULL,read,NULL);
 42     pthread_create(&writer,NULL,write,NULL);
 43 
 44     pthread_join(reader,NULL);
 45     pthread_join(writer,NULL);
 46     pthread_rwlock_destroy(&rwlock);
 47     return 0;
 48 }

(2)写者可以写成功,sleep 3秒,但同时1秒后读者到达,但是读者没有读成功,因为写者在sleep,并没有释放锁

  1 #include<stdio.h>
  2 #include<pthread.h>
  3 int book;
  4 pthread_rwlock_t rwlock;
  5 void* read(void* arg)
  6 {
  7     sleep(1);//(2)
  8     while(1)
  9     {
 10         if(pthread_rwlock_tryrdlock(&rwlock)!=0)
 11         {
 12             printf("writer is writing..!\n");
 13         }else{
 14             printf("read book is:%d\n",book);
 15//           sleep(3);//(1)
 16             pthread_rwlock_unlock(&rwlock);
 17         }
 18     }
 19 }
 20 
 21 void* write(void* arg)
 22 {
 23//   sleep(1);//(1)
 24     while(1)
 25     {
 26         if(pthread_rwlock_trywrlock(&rwlock)!=0)
 27         {
 28             printf("reader is reading..!\n");
 29         }else{
 30             book++;
 31             printf("write done:%d\n",book);
 32             sleep(3);//(2)
 33             pthread_rwlock_unlock(&rwlock);
 34         }
 35     }
 36 }
 37 int main()
 38 {
 39     pthread_rwlock_init(&rwlock,NULL);
 40     pthread_t reader,writer;
 41     pthread_create(&reader,NULL,read,NULL);
 42     pthread_create(&writer,NULL,write,NULL);
 43 
 44     pthread_join(reader,NULL);
 45     pthread_join(writer,NULL);
 46     pthread_rwlock_destroy(&rwlock);
 47     return 0;
 48 }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值