用信号量实现的线程安全队列。
简单有用的示例程序,
比起互斥量的实现在多线程时效率更好。
cir_queue.h
- /*
- * \File
- * cir_queue.h
- * \Brief
- * circular queue
- */
- #ifndef __CIR_QUEUE_H__
- #define __CIR_QUEUE_H__
- #define QUE_SIZE 8
- typedef int DataType;
- typedef struct cir_queue_t
- {
- DataType data[QUE_SIZE];
- int front;
- int rear;
- int count;
- }cir_queue_t;
- extern sem_t queue_sem;
- void init_cir_queue(cir_queue_t* q);
- int is_empty_cir_queue(cir_queue_t* q);
- int is_full_cir_queue(cir_queue_t* q);
- void push_cir_queue(cir_queue_t* q, DataType x);
- DataType pop_cir_queue(cir_queue_t* q);
- DataType top_cir_queue(cir_queue_t* q);
- void destroy_cir_queue(cir_queue_t* q);
- void print_queue(cir_queue_t* q);
- #endif
main.c
- /*
- * \File
- * main.c
- * \Breif
- * Thread-safe circular-queue implemented by semaphore
- * \Author
- * Hank.yan
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <pthread.h>
- #include <semaphore.h>
- #include "cir_queue.h"
- void* thread_queue(void *arg);
-
- /*
- * \Func
- * main
- */
- int main(int argc, char* argv[])
- {
- int res;
- cir_queue_t cq;
- DataType e;
-
- pthread_t a_thread, b_thread;
- void* thread_result;
- init_cir_queue(&cq);
- push_cir_queue(&cq, 1);
- push_cir_queue(&cq, 2);
- push_cir_queue(&cq, 3);
- print_queue(&cq);
- res = pthread_create(&a_thread, NULL, thread_queue, (void*)&cq);
- if (res != 0)
- {
- perror("Thread creation failed.");
- exit(EXIT_FAILURE);
- }
- e = pop_cir_queue(&cq);
- e = pop_cir_queue(&cq);
- print_queue(&cq);
- push_cir_queue(&cq, 9);
- push_cir_queue(&cq, 100);
- print_queue(&cq);
- res = pthread_create(&b_thread, NULL, thread_queue, (void*)&cq);
- if (res != 0)
- {
- perror("Thread creation failed.");
- exit(EXIT_FAILURE);
- }
- e = pop_cir_queue(&cq);
- push_cir_queue(&cq, 20);
- print_queue(&cq);
- printf("Waiting for thread to finish...\n");
- res = pthread_join(a_thread, &thread_result);
- if (res != 0)
- {
- perror("Thread join failed.");
- exit(EXIT_FAILURE);
- }
- print_queue(&cq);
- printf("Waiting for thread to finish...\n");
- res = pthread_join(b_thread, &thread_result);
- if (res != 0)
- {
- perror("Thread join failed.");
- exit(EXIT_FAILURE);
- }
- destroy_cir_queue(&cq);
- printf("Thread joined, it returned %s\n", (char*)thread_result);
- exit(EXIT_SUCCESS);
- }
- void *thread_queue(void *cirqueue)
- {
- int flag;
- DataType element;
- print_queue((cir_queue_t*)cirqueue);
- flag = is_empty_cir_queue((cir_queue_t*)cirqueue);
- print_queue((cir_queue_t*)cirqueue);
- element = pop_cir_queue((cir_queue_t*)cirqueue);
- element = pop_cir_queue((cir_queue_t*)cirqueue);
- print_queue((cir_queue_t*)cirqueue);
- push_cir_queue((cir_queue_t*)cirqueue, 5);
- print_queue((cir_queue_t*)cirqueue);
- push_cir_queue((cir_queue_t*)cirqueue, 99);
- push_cir_queue((cir_queue_t*)cirqueue, 1000);
- push_cir_queue((cir_queue_t*)cirqueue, 88);
- print_queue((cir_queue_t*)cirqueue);
-
- pthread_exit("Thank you for the cpu time.");
- }
cir_queue.c
makefile
- OBJECTS = main.o cir_queue.o
- CC = gcc
- CFLAGS = -D_REENTRANT -lpthread -g -Wall
- thrd_safe_queue: $(OBJECTS)
- $(CC) $(CFLAGS) -o thrd_safe_queue $(OBJECTS)
- main.o: cir_queue.h
- cir_queue.o: cir_queue.h
- .PHONY:clean
- clean:
- rm thrd_safe_queue $(OBJECTS)
阅读(313) | 评论(0) | 转发(0) |