Advertisement
Guest User

Untitled

a guest
Aug 25th, 2012
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4. #include "t_queue.h"
  5.  
  6. queue* new_q()
  7. {
  8. queue *queue_n;
  9. q_lock lock;
  10.  
  11. queue_n = (queue *)malloc(sizeof(queue));
  12.  
  13. queue_n->front = NULL;
  14. queue_n->rear = NULL;
  15. queue_n->lock = lock;
  16.  
  17. pthread_mutex_init(&queue_n->lock, NULL);
  18.  
  19. return queue_n;
  20. }
  21.  
  22. void del_q(queue *q)
  23. {
  24.  
  25. pthread_mutex_lock(&q->lock);
  26.  
  27. node *next, *del;
  28.  
  29. next = q->front;
  30. while(next!=NULL)
  31. {
  32. del = next;
  33. next = next->next;
  34. free(del);
  35. }
  36.  
  37. pthread_mutex_unlock(&q->lock);
  38. pthread_mutex_destroy(&q->lock);
  39. free(q);
  40. }
  41.  
  42. void put_q(queue *q, element data)
  43. {
  44. pthread_mutex_lock(&q->lock);
  45.  
  46. node *node_n;
  47. node_n = (node *)malloc(sizeof(node));
  48.  
  49. node_n->data = data;
  50. node_n->next = NULL;
  51.  
  52. if(q->rear != NULL)
  53. q->rear->next = node_n;
  54. q->rear = node_n;
  55.  
  56. if(q->front==NULL)
  57. q->front = node_n;
  58.  
  59. pthread_mutex_unlock(&q->lock);
  60.  
  61. }
  62.  
  63. element *get_q(queue *q, element *d)
  64. {
  65.  
  66. pthread_mutex_lock(&q->lock);
  67.  
  68. node *node_n;
  69. element ret;
  70.  
  71. if(q->rear != NULL && q->front != NULL)
  72. {
  73. node_n = q->front;
  74. *d = node_n->data;
  75. q->front = node_n->next;
  76. free(node_n); //this causes the error i think
  77. }
  78. else
  79. {
  80. d = NULL;
  81. }
  82.  
  83. pthread_mutex_unlock(&q->lock);
  84. return d;
  85. }
  86.  
  87. void print_q(queue *q)
  88. {
  89. pthread_mutex_lock(&q->lock);
  90.  
  91. node *next;
  92.  
  93. next = q->front;
  94. while(next!=NULL)
  95. {
  96. printf(":%d\n", next->data);
  97. next = next->next;
  98. }
  99.  
  100. pthread_mutex_unlock(&q->lock);
  101.  
  102. }
  103.  
  104. int empty_q(queue *q)
  105. {
  106.  
  107. pthread_mutex_lock(&q->lock);
  108.  
  109. if(q->front==NULL)
  110. {
  111. pthread_mutex_unlock(&q->lock);
  112. return 1;
  113. }
  114. else
  115. {
  116. pthread_mutex_unlock(&q->lock);
  117. return 0;
  118. }
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement