当前位置:   article > 正文

关于读写锁以及用互斥锁和条件变量实现读写锁(写优先)_用读写锁的时候要用互斥锁保护吗

用读写锁的时候要用互斥锁保护吗
读写锁包括读取锁和写入锁,多个读线程可以同时访问共享数据;
写线程必须等待所有读线程都释放锁以后,才能取得锁;
同样的,读线程必须等待写线程释放锁后,才能取得锁;
也就是说读写锁要确保的是如下互斥关系:可以同时读,但是读-写,写-写都是互斥的;
明白这一点就很好做了。

读写锁的分配规则:
1.只要没有线程持有某个给定的读写锁用于写,那么任意数目的线程可以持有该读写锁写锁用于读。
2.仅当没有线程持有某个给定的的读写锁用于读或者用于写时,才能分配该读写锁用于写。

通俗点说就是当没有写锁时,就可以加读锁且任意线程可以同时加,而写锁只能有一个,且必须在没有读锁时才能加上,一般来说,写锁优先。
读写锁的数据类型为pthread_rwlock_t,可以用PTHREAD_RWLOCK_INITIALIZER来初始化。

  1. #include<pthread.h>
  2. int pthread_rwlock_rdlock(pthread_rwlock_t *rwptr);
  3. int pthread_rwlock_wrlock(pthread_rwlock_t *rwptr);
  4. int pthread_rwlock_unlock(pthread_rwlock_t *rwptr);

以上三个基本函数返回值均:成功返回0,出错返回正的错误值;

  1. #include<pthread.h>
  2. int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwptr);
  3. int pthread_rwlock_trywrlock(pthread_rwlock_t *rwptr);

返回值同上;
要注意的是,上面两个函数尝试获取一个读出锁或者写入锁,但是如果该锁不能马上获得,那就返回一个EBUSY错误,而不是把调用线程投入睡眠。


如何用互斥锁和条件变量实现读写锁呢?

以写锁优先为例:
先定义基本的pthread_rwlock_t数据类型和操做读写锁的各个函数的函数原型,即my_pthread_rwlock.h头文件;
  1. #pragma once
  2. #include<pthread.h>
  3. #include<stdio.h>
  4. #include<errno.h>
  5. #include<stdlib.h>
  6. typedef struct
  7. {
  8. pthread_mutex_t rw_mutex;
  9. pthread_cond_t rw_condreaders;
  10. pthread_cond_t rw_condwriters;
  11. int rw_magic;
  12. int rw_nwaitreaders;
  13. int rw_nwaitwriters;
  14. int rw_refcount; // 0 >0 ==-1
  15. }my_pthread_rwlock_t;
  16. #define RW_MAGIC 0x19960511 //魔法数,可修改
  17. #define MY_PTHREAD_RWLOCK_INITIALIZER {PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, PTHREAD_COND_INITIALIZER,\
  18. RW_MAGIC,0,0,0}
  19. typedef int my_pthread_rwlockattr_t;
  20. int my_pthread_rwlock_init(my_pthread_rwlock_t *rw, my_pthread_rwlockattr_t *attr);
  21. int my_pthread_rwlock_destroy(my_pthread_rwlock_t *rw);
  22. int my_pthread_rwlock_rdlock(my_pthread_rwlock_t *rw);
  23. int my_pthread_rwlock_wrlock(my_pthread_rwlock_t *rw);
  24. int my_pthread_rwlock_unlock(my_pthread_rwlock_t *rw);
  25. int my_pthread_rwlock_tryrdlock(my_pthread_rwlock_t *rw);
  26. int my_pthread_rwlock_trywrlock(my_pthread_rwlock_t *rw);

函数实现部分:

  1. #include"my_pthread_rwlock.h"
  2. int my_pthread_rwlock_init(my_pthread_rwlock_t *rw, my_pthread_rwlockattr_t *attr)
  3. {
  4. int result;
  5. if(attr != NULL)
  6. return -1;
  7. if((result = pthread_mutex_init(&rw->rw_mutex, NULL)) != 0)
  8. goto err1;
  9. if((result = pthread_cond_init(&rw->rw_condreaders, NULL)) != 0)
  10. goto err2;
  11. if((result = pthread_cond_init(&rw->rw_condwriters, NULL)) != 0)
  12. goto err3;
  13. rw->rw_nwaitreaders = 0;
  14. rw->rw_nwaitwriters = 0;
  15. rw->rw_refcount = 0;
  16. rw->rw_magic = RW_MAGIC;
  17. return 0;
  18. err3:
  19. pthread_cond_destroy(&rw->rw_condreaders);
  20. err2:
  21. pthread_cond_destroy(&rw->rw_mutex);
  22. err1:
  23. return result;
  24. }
  25. int my_pthread_rwlock_rdlock(my_pthread_rwlock_t *rw)
  26. {
  27. int result;
  28. if(rw->rw_magic != RW_MAGIC)
  29. return -1;
  30. if((result = pthread_mutex_lock(&rw->rw_mutex)) != 0)
  31. return result;
  32. while(rw->rw_refcount<0 || rw->rw_nwaitwriters>0)//在有写锁的条件下等待
  33. {
  34. rw->rw_nwaitreaders++;
  35. result = pthread_cond_wait(&rw->rw_condreaders, &rw->rw_mutex);
  36. rw->rw_nwaitreaders--;
  37. if(result != 0)
  38. break;
  39. }
  40. if(result == 0)
  41. rw->rw_refcount++;
  42. pthread_mutex_unlock(&rw->rw_mutex);
  43. return result;
  44. }
  45. int my_pthread_rwlock_wrlock(my_pthread_rwlock_t *rw)
  46. {
  47. int result;
  48. if(rw->rw_magic != RW_MAGIC)
  49. return -1;
  50. if((result = pthread_mutex_lock(&rw->rw_mutex)) != 0)
  51. return result;
  52. while(rw->rw_refcount != 0)//有锁时等待
  53. {
  54. rw->rw_nwaitwriters++;
  55. result = pthread_cond_wait(&rw->rw_condwriters, &rw->rw_mutex);
  56. rw->rw_nwaitwriters--;
  57. if(result != 0)
  58. break;
  59. }
  60. if(result == 0)
  61. rw->rw_refcount = -1;
  62. pthread_mutex_unlock(&rw->rw_mutex);
  63. return result;
  64. }
  65. int my_pthread_rwlock_unlock(my_pthread_rwlock_t *rw)
  66. {
  67. int result;
  68. if(rw->rw_magic != RW_MAGIC)
  69. return -1;
  70. if((result = pthread_mutex_lock(&rw->rw_mutex)) != 0)
  71. return result;
  72. if(rw->rw_refcount > 0)
  73. rw->rw_refcount--;
  74. else if(rw->rw_refcount == -1)
  75. rw->rw_refcount = 0;
  76. else
  77. printf("unlock error.\n");
  78. if(rw->rw_nwaitwriters > 0)//写锁优先
  79. {
  80. if(rw->rw_refcount == 0)
  81. {
  82. result = pthread_cond_signal(&rw->rw_condwriters);
  83. }
  84. }
  85. else if(rw->rw_nwaitreaders > 0)//唤醒读锁
  86. result = pthread_cond_broadcast(&rw->rw_condreaders);
  87. pthread_mutex_unlock(&rw->rw_mutex);
  88. return result;
  89. }
  90. int my_pthread_rwlock_tryrdlock(my_pthread_rwlock_t *rw)
  91. {
  92. int result;
  93. if(rw->rw_magic != RW_MAGIC)
  94. return -1;
  95. if((result = pthread_mutex_lock(&rw->rw_mutex)) != 0)
  96. return result;
  97. if(rw->rw_refcount < 0 || rw->rw_nwaitwriters > 0)
  98. result = EBUSY;
  99. else
  100. rw->rw_refcount++;
  101. pthread_mutex_unlock(&rw->rw_mutex);
  102. return result;
  103. }
  104. int my_pthread_rwlock_trywrlock(my_pthread_rwlock_t *rw)
  105. {
  106. int result;
  107. if(rw->rw_magic != RW_MAGIC)
  108. return -1;
  109. if((result = pthread_mutex_lock(&rw->rw_mutex)) != 0)
  110. return result;
  111. if(rw->rw_refcount != 0)
  112. result = EBUSY;
  113. else
  114. rw->rw_refcount = -1;
  115. pthread_mutex_unlock(&rw->rw_mutex);
  116. return result;
  117. }
  118. int my_pthread_rwlock_destroy(my_pthread_rwlock_t *rw)
  119. {
  120. if(rw->rw_magic != RW_MAGIC)
  121. return -1;
  122. if(rw->rw_refcount != 0 || rw->rw_nwaitwriters != 0)
  123. return EBUSY;
  124. pthread_mutex_destroy(&rw->rw_mutex);
  125. pthread_cond_destroy(&rw->rw_condreaders);
  126. pthread_cond_destroy(&rw->rw_condwriters);
  127. rw->rw_magic = 0;
  128. return 0;
  129. }

再是测试程序:

  1. #include<stdio.h>
  2. #include<unistd.h>
  3. #include<pthread.h>
  4. #include"my_pthread_rwlock.h"
  5. my_pthread_rwlock_t rwlock = MY_PTHREAD_RWLOCK_INITIALIZER;
  6. void * thread_fun1(void *arg)
  7. {
  8. my_pthread_rwlock_wrlock(&rwlock);
  9. printf("thread 1 wrlock.\n");
  10. sleep(3);
  11. my_pthread_rwlock_unlock(&rwlock);
  12. }
  13. void * thread_fun2(void *arg)
  14. {
  15. my_pthread_rwlock_wrlock(&rwlock);
  16. printf("thread 2 wrlock.\n");
  17. my_pthread_rwlock_unlock(&rwlock);
  18. }
  19. void * thread_fun3(void *arg)
  20. {
  21. my_pthread_rwlock_rdlock(&rwlock);
  22. printf("thread 3 rdlock.\n");
  23. my_pthread_rwlock_unlock(&rwlock);
  24. }
  25. int main()
  26. {
  27. pthread_t tid1, tid2, tid3;
  28. pthread_create(&tid1, NULL, thread_fun1, NULL);
  29. sleep(1);
  30. pthread_create(&tid3, NULL, thread_fun3, NULL);
  31. pthread_create(&tid2, NULL, thread_fun2, NULL);
  32. pthread_join(tid1, NULL);
  33. pthread_join(tid2, NULL);
  34. pthread_join(tid3, NULL);
  35. return 0;
  36. }

结果:
这样子一个写优先的读写锁就实现了,读优先的话也是同样原理的,只是部分代码的执行顺序和部分条件判定不同而已


事与冀盼又落差,请不必惊怕
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/336606?site
推荐阅读
相关标签
  

闽ICP备14008679号