当前位置:   article > 正文

Linux 线程同步2_linux线程同步二头歌

linux线程同步二头歌

线程同步2


读写锁

读写锁与互斥锁类似,不过读写锁允许更高的并行性。读写锁可以有三种状态:(1) 读模式下的加锁状态;(2)写模式下的加锁状态(3)不加锁状态。这个区别与互斥锁的,因为互斥锁只有加锁和不加锁的两种状态。一次只能有一个线程的写状态的读写锁,但是可以有多个线程占有读状态的读写锁。

初始化和清理

#include <pthread.h>
int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock,
const pthread_rwlockattr_t *restrict attr);

int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);

功能

读写锁通过调用pthread_rwlock_init进行初始化的,attr =NULL时使用缺省的属性。在释放读写锁占用的内存之前,需要调用pthread_rwlock_destroy函数做清理工作。

加锁和解锁


#include <pthread.h>
int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);  //写模式下的加锁
int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);  //读模式下的加锁
int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);  //无论那种模式都可以用的解锁


unix环境高级编程的例子

  1. //使用读写锁
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4. struct job
  5. {
  6. struct job *j_next;
  7. struct job *j_prev;
  8. pthread_t j_id;
  9. };
  10. struct queue
  11. {
  12. struct job *q_head;
  13. struct job *q_tail;
  14. pthread_rwlock_t q_lock;
  15. };
  16. int queue_init(struct queue *qp)
  17. {
  18. int err;
  19. qp->q_head = NULL;
  20. qp->q_tail = NULL;
  21. err= pthread_rwlock_init(&qp->q_lock, NULL);
  22. if(err != 0)
  23. {
  24. return(err);
  25. }
  26. return(0);
  27. }
  28. void job_insert(struct queue *qp, struct job *jp)
  29. {
  30. pthread_rwlock_wrlock(&qp->q_lock);
  31. jp->j_next = qp->q_head;
  32. jp->j_prev = NULL;
  33. if(qp->q_head != NULL)
  34. {
  35. qp->q_head->j_prev = jp;
  36. }
  37. else
  38. {
  39. qp->q_tail = jp;
  40. }
  41. qp->q_head = jp;
  42. pthread_rwlock_unlock(&qp->q_lock);
  43. }
  44. void job_append(struct queue *qp, struct job *jp)
  45. {
  46. pthread_rwlock_wrlock(&qp->q_lock);
  47. jp->j_next = NULL;
  48. jp->j_prev = qp->q_tail;
  49. if(qp->q_tail != NULL)
  50. {
  51. qp->q_tail->j_next = jp;
  52. }
  53. else
  54. {
  55. qp->q_head = jp;
  56. }
  57. qp->q_tail = jp;
  58. pthread_rwlock_unlock(&qp->q_lock);
  59. }
  60. void job_remove(struct queue *qp,struct job *jp)
  61. {
  62. pthread_rwlock_wrlock(&qp->q_lock);
  63. if(jp == qp_q_head)
  64. {
  65. qp->q_head = jp->next;
  66. if(qp->q_tail == jp)
  67. {
  68. qp->q_tail = NULL;
  69. }
  70. }
  71. else if(jp == qp->q_tail)
  72. {
  73. qp->q_tail = jp->j_prev;
  74. if(qp->q_head == jp)
  75. {
  76. qp->q_head = NULL;
  77. }
  78. }
  79. else
  80. {
  81. jp->j_prev->j_next = jp->j_next;
  82. jp->j_next->j_prev = jp->j_prev;
  83. }
  84. pthread_rwlock_unlock(&qp->q_lock);
  85. }
  86. struct job *job_find(struct queue *qp, pthread_t id)
  87. {
  88. struct job *jp;
  89. if(pthread_rwlock_rdlock(&qp->q_lock) != 0)
  90. {
  91. return(NULL);
  92. }
  93. for(jp = qp->q_head; jp != NULL; jp = ip->j_next)
  94. {
  95. if(pthread_equal(jp->j_id, id))
  96. {
  97. break;
  98. }
  99. }
  100. pthread_rwlock_unlock(&qp->q_lock);
  101. return(jp);
  102. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/336663
推荐阅读
相关标签
  

闽ICP备14008679号