[root@TEST comm_queue]# cat queue.h
- #ifndef MP4_QUEUE_H
- #define MP4_QUEUE_H
- #include <assert.h>
- #include <pthread.h>
- #include <stdio.h>
- #include <stdlib.h>
- typedef enum {
- QUEUE_EMPTY,
- QUEUE_REGULAR
- } queue_flag;
- typedef struct _qnode_t qnode_t;
- struct _qnode_t {
- int fd;
- qnode_t *next;
- };
- typedef struct _queue_t queue_t;
- struct _queue_t {
- qnode_t *head;
- qnode_t *tail;
- };
- //extern queue_t queue;
- extern queue_flag queue_test(queue_t *queue);
- extern void enqueue(queue_t *queue, int fd);
- extern int dequeue(queue_t *queue);
- extern void traverse(queue_t *q);
- #endif
[root@TEST_190 comm_queue]# cat queue.c
- #include "queue.h"
- queue_flag queue_test(queue_t *queue)
- {
- assert(queue);
- if (queue->head == NULL)
- return QUEUE_EMPTY;
- return QUEUE_REGULAR;
- }
- void enqueue(queue_t *queue,int fd)
- {
- assert(queue);
- qnode_t *node = NULL;
- node = (qnode_t*)malloc(sizeof(qnode_t));
- if (!node) {
- perror("enqueue malloc error:\n");
- return;
- }
- node->fd = fd;
- node->next = NULL;
- if (queue->tail == NULL) {
- queue->head = queue->tail = node;
- return;
- }
- queue->tail->next = node;
- queue->tail = node;
- }
- int dequeue(queue_t *queue)
- {
- assert(queue);
- if (queue->tail == NULL && queue->head == NULL) {
- //printf("dequeue() queue is empty!\n");
- return -1;
- }
- qnode_t *tmp = NULL;
- int ret = -1;
- tmp = queue->head;
- ret = tmp->fd;
- queue->head = tmp->next;
- //如果出对到队尾了,需要将队尾更新为空,以便于后续再次执行入对操作
- if (queue->tail == tmp)
- queue->tail = queue->tail->next;
- free(tmp);
- return ret;
- }
- void traverse(queue_t *q)
- {
- unsigned long pid = pthread_self();
- if (q->head == NULL) {
- printf("traverse() {%d} queue is empty!\n",pid);
- return;
- }
-
- qnode_t *pos = NULL;
- int i = 0;
- for (pos = q->head;pos != NULL; pos = pos->next,i++) {
- printf("traverse{%lu} pos[%d]=%d\n",pid,i,pos->fd);
- }
- }
验证程序
[root@TEST_190 comm_queue]# cat main.c
- #include "queue.h"
- int count = 10000;
- pthread_mutex_t m;
- queue_t queue = {NULL, NULL};
- void *writer_thread(void *param);
- void *reader_thread(void *param);
- int main(void)
- {
- struct timeval start,end;
- gettimeofday(&start, NULL);
- pthread_t writer,reader;
- pthread_mutex_init(&m, NULL);
- pthread_create(&writer, NULL, writer_thread, NULL);
- pthread_create(&reader, NULL, reader_thread, NULL);
- pthread_join(writer, NULL);
- pthread_join(reader, NULL);
- gettimeofday(&end, NULL);
- double elasp = (end.tv_sec - start.tv_sec) * 1000000.0 + (end.tv_usec - start.tv_usec);
- printf("pthread id = %lu, elasp = %f\n", pthread_self(), elasp);
- //traverse(&queue);
- return 0;
- }
- void *writer_thread(void *param)
- {
- int i;
- for (i = 0; i < count; i++ ) {
- pthread_mutex_lock(&m);
- enqueue(&queue,i);
- printf("enqueue [%d]\n",i);
- pthread_mutex_unlock(&m);
- }
- //traverse(&queue);
- }
- void *reader_thread(void *param)
- {
- int ret=0,i;
- //模拟无限出队的情况,暂时执行次数为 100*count
- for(i=0;i<100*count;i++) {
- pthread_mutex_lock(&m);
- ret = dequeue(&queue);
- if (ret == -1) {
- printf(".");
- } else {
- printf("dequeue ret = %d\n",ret);
- }
-
- pthread_mutex_unlock(&m);
- }
- //traverse(&queue);
- }
编译和确认,需要查看log确认,队列为空时的出队操作,会打出“.”:
- gcc queue.c main.c -o q1 -g -lpthread
- ./q1 | tee log
阅读(2243) | 评论(1) | 转发(1) |