• 多线程 线程队列的实现


    参考他人的方法,自己做了简单修改,实现一个通用的线程队列。


    #include <stdio.h>
    #include <pthread.h>
    #include <unistd.h>
    
    #define POOL_SIZE 10
    #define QUEUE_LEN 20
    pthread_mutex_t queue_lock; //队列锁
    pthread_cond_t  queue_cond; //条件变量
    
    int head = 0, tail = 0;
    int queue[QUEUE_LEN];
    
    void delay(int x)
    {
    	int i,j;
    	for(i = 0; i < x; i++)
    		for(j = 0; j <x; j++);
    }
    
    void fun1(){
    	printf("fun1 start\n");
    	delay(1000);
    	printf("fun1 end\n");
    }
    
    void fun2(){
    	printf("fun2 start\n");
    	delay(2000);
    	printf("fun2 end\n");
    }
    
    void fun3(){
    	printf("fun3 start\n");
    	delay(3000);
    	printf("fun3 end\n");
    }
    
    void fun4(){
    	printf("fun4 start\n");
    	delay(4000);
    	printf("fun4 end\n");
    }
    
    int dequeue()//出对列函数
    {
    	int y;
    	pthread_mutex_lock(&queue_lock);
    	while(head == tail)//队列中 无情求时,等待
    	{
    		pthread_cond_wait(&queue_cond, &queue_lock);//自动释放锁		
    	}
    	y = queue[++head];//需做循环处理  queue[head] is NULL
    	if(head >= QUEUE_LEN)
    	  head = 0;
    	pthread_mutex_unlock(&queue_lock);
    	return y;
    }
    
    void * process_queue(void *arg)
    {
    	int x = 0;
    	for(;;)
    	{
    		x = dequeue();
    		printf("%d dequeue \n",x);
    		switch(x)
    		{
    			case 1:
    				fun1(); break;
    			case 2:
    				fun2(); break;
    		    case 3:
    				fun3(); break;
    			default:
    				fun4(); break;
    		}		
    	}
    }
    
    int enqueue(int x)//插入请求 
    {
    	int next;
    	pthread_mutex_lock(&queue_lock);
    	next = tail + 1;
    	if(next >= QUEUE_LEN)
    		next = 0;
    	if(next == head)
    	{
    	 pthread_cond_signal(&queue_cond);//
    	 pthread_mutex_unlock(&queue_lock);   
    	 return 0;// 表示队列已满
    	}
    	queue[next] = x;
    	tail = next;	
    	pthread_cond_signal(&queue_cond);//激活
    	pthread_mutex_unlock(&queue_lock);
    	return 1;//入队成功		
    }
    
    int main()
    {
    	int i;
    	int num = 0;
    	pthread_t tid[POOL_SIZE];
    
    	pthread_mutex_init(&queue_lock, NULL);
    	pthread_cond_init(&queue_cond, NULL);
    
    	for(i = 0; i < POOL_SIZE; i++)
    		pthread_create(&tid[i], NULL, process_queue, NULL);
    
    	while(1)
    	{
    		scanf("%d",&num);
    		if(0 == num)
    		//	break;
    			return 0;
    		printf("%d ",num);
    		while(!enqueue(num))//队列满时 等待
    		{
    			printf("queue is full\n");
    			sleep(1);
    		}
    	}
    
    //	for(i = 0; i < POOL_SIZE; i++)
    //		pthread_join(tid[i], NULL);
    
    //	pthread_mutex_destory(&queue_lock);
    //	pthread_cond_destroy(&queue_cond);
    
    	return 0;
    }

    还有一些不足之处,随后补上。


    测试方法:

    键入 :

    4 3 2 1

    结果:
    4 3 2 1 4 dequeue
    fun4 start
    3 dequeue
    fun3 start
    2 dequeue
    fun2 start
    1 dequeue
    fun1 start
    fun1 end
    fun2 end
    fun3 end
    fun4 end

    键入:

    0

    退出函数

  • 相关阅读:
    配置Tomcat 输入ip直接访问自己的页面
    为什么hashMap的容量是2的幂次
    LinkedList源码详解
    以太网和Zigbee的家居信息采集系统
    无线LED智能照明控制系统
    ZigBee无线应变采集装置
    ZigBee红外远程监控系统设计
    ZigBee教室照明监控系统设计
    基于物联网的智能医护系统研究
    物联网的低成本乳品质量链追溯平台设计
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3020163.html
Copyright © 2020-2023  润新知