Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include "t_queue.h"
- queue* new_q()
- {
- queue *queue_n;
- q_lock lock;
- queue_n = (queue *)malloc(sizeof(queue));
- queue_n->front = NULL;
- queue_n->rear = NULL;
- queue_n->lock = lock;
- pthread_mutex_init(&queue_n->lock, NULL);
- return queue_n;
- }
- void del_q(queue *q)
- {
- pthread_mutex_lock(&q->lock);
- node *next, *del;
- next = q->front;
- while(next!=NULL)
- {
- del = next;
- next = next->next;
- free(del);
- }
- pthread_mutex_unlock(&q->lock);
- pthread_mutex_destroy(&q->lock);
- free(q);
- }
- void put_q(queue *q, element data)
- {
- pthread_mutex_lock(&q->lock);
- node *node_n;
- node_n = (node *)malloc(sizeof(node));
- node_n->data = data;
- node_n->next = NULL;
- if(q->rear != NULL)
- q->rear->next = node_n;
- q->rear = node_n;
- if(q->front==NULL)
- q->front = node_n;
- pthread_mutex_unlock(&q->lock);
- }
- element *get_q(queue *q, element *d)
- {
- pthread_mutex_lock(&q->lock);
- node *node_n;
- element ret;
- if(q->rear != NULL && q->front != NULL)
- {
- node_n = q->front;
- *d = node_n->data;
- q->front = node_n->next;
- free(node_n); //this causes the error i think
- }
- else
- {
- d = NULL;
- }
- pthread_mutex_unlock(&q->lock);
- return d;
- }
- void print_q(queue *q)
- {
- pthread_mutex_lock(&q->lock);
- node *next;
- next = q->front;
- while(next!=NULL)
- {
- printf(":%d\n", next->data);
- next = next->next;
- }
- pthread_mutex_unlock(&q->lock);
- }
- int empty_q(queue *q)
- {
- pthread_mutex_lock(&q->lock);
- if(q->front==NULL)
- {
- pthread_mutex_unlock(&q->lock);
- return 1;
- }
- else
- {
- pthread_mutex_unlock(&q->lock);
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement