Chinaunix首页 | 论坛 | 博客
  • 博客访问: 581067
  • 博文数量: 104
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1559
  • 用 户 组: 普通用户
  • 注册时间: 2014-08-21 00:58
个人简介

锻炼精神,首先要锻炼肉体

文章分类

全部博文(104)

文章存档

2018年(1)

2016年(1)

2015年(101)

2014年(1)

我的朋友

分类: C/C++

2015-02-25 17:26:09

头文件:
#include
函数模型:
int pthread_rwlock_rdlock ( pthread_rwlock_t *rwlock ) ;
int pthread_rwlock_wrlock (pthread_rwlock_t *rwlock ) ;
int pthread_rwlock_unlock (pthread_rwlock_t *rwlock ) ;



点击(此处)折叠或打开

  1. #include <stdlib.h>
  2. #include <pthread.h>

  3. struct job
  4. {
  5.     struct job *j_next ;
  6.     struct job *j_prev ;
  7.     pthread_t j_id ;
  8.     // tells which thread handles this job
  9. } ;

  10. struct queue
  11. {
  12.     struct job *q_head ;
  13.     struct job *q_tail ;
  14.     pthread_rwlock_t q_lock ;
  15. } ;

  16. // initialize a queue

  17. int queue_init ( struct queue *qp )
  18. {
  19.     int err ;
  20.     
  21.     qp->q_head = NULL ;
  22.     qp->q_tail = NULL ;
  23.     
  24.     err = pthread_rwlock_init ( &qp->q_lock, NULL ) ;
  25.     if ( err != 0 )
  26.         return (err) ;
  27.     
  28.     return (0) ;
  29. }

  30. /**
  31. Insert a job at the head of the queue
  32. */
  33. void job_insert ( struct queue *qp , struct job *jp )
  34. {
  35.     pthread_rwlock_wrlock ( &qp->q_lock ) ;
  36.     jp->j_next = qp->q_head ;
  37.     jp->j_prev = NULL ;
  38.     if ( qp->q_head != NULL )
  39.             qp->q_head->j_prev = jp ;
  40.     else
  41.             qp->q_tail = jp ;
  42.     qp->q_head = jp ;
  43.     pthread_rwlock_unlock (&qp->q_lock) ;
  44. }

  45. /**
  46. Append a job on the tail of the queue
  47. */

  48. void job_append ( struct queue *qp , struct job *jp )
  49. {
  50.     pthread_rwlock_wrlock ( &qp->q_lock ) ;
  51.     jp->j_next = NULL ;
  52.     jp->j_prev = qp->q_tail ;
  53.     
  54.     if ( qp->q_tail != NULL )
  55.         qp->q_tail->j_next = jp ;
  56.     else
  57.         qp->q_head = jp ;
  58.     qp->q_tail = jp ;
  59.     pthread_rwlock_wrlock (&qp->q_lock) ;
  60. }

  61. // remove the given job from a queue

  62. void job_remove ( struct queue *qp , struct job *jp )
  63. {
  64.     pthread_rwlock_wrlock (&qp->q_lock) ;
  65.     if ( jp == qp->q_head )
  66.     {
  67.         qp->q_head = jp->j_next ;
  68.         if ( qp->q_tail == jp )
  69.             qp->q_tail = NULL ;
  70.         else
  71.             jp->j_next->j_prev = jp->j_prev ;
  72.     }
  73.     else if ( jp == qp->q_tail )
  74.     {
  75.         qp->q_tail = jp->j_prev ;
  76.         jp->j_prev->j_next = jp->j_next ;
  77.     }
  78.     else
  79.     {
  80.         jp->j_prev->j_next = jp->j_next ;
  81.         jp->j_next->prev = jp->j_prev ;
  82.     }
  83.     pthread_rwlock_unlock (&qp->q_lock) ;
  84. }

  85. struct job *job_find ( struct queue *qp , pthread_t id )
  86. {
  87.     struct job *jp ;
  88.     if ( pthread_rwlock_rdlock ( &qp->q_lock ) != 0 )
  89.         return NULL ;
  90.     for ( jp = qp->q_head ; jp != NULL ; jp= jp->j_next )
  91.             if ( pthread_equal ( jp->j_id , id ))
  92.                     break ;
  93.     pthread_rwlock_unlock ( &qp->q_lock ) ;
  94.     return (jp) ;
  95. }


阅读(1460) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~