参考他人的方法,自己做了简单修改,实现一个通用的线程队列。
#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++]; //head first element,tail points to the back of last one
if(head >= QUEUE_LEN) //0~ QUEUE_LEN-1
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)//插入请求
{
pthread_mutex_lock(&queue_lock);
queue[tail] = x;
++tail;
if(tail >= QUEUE_LEN)
tail = 0;
if(tail == head)
{
pthread_cond_signal(&queue_cond);//
pthread_mutex_unlock(&queue_lock);
return 0;// 表示队列已满
}
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;
printf("%d ",num);
while(!enqueue(num))//队列满时 等待
{
printf("queue is full\n");
sleep(1);
}
}
for(i = 0; i < POOL_SIZE; i++)
pthread_cancel(tid[i]);//发出请求
// for(i = 0; i < POOL_SIZE; i++)
// pthread_join(tid[i], NULL); // 为何不能取得退出状态???
pthread_mutex_destroy(&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
退出函数
分享到:
相关推荐
java多线程模拟队列实现排队叫号,多线程模拟排队叫号取号 java多线程模拟队列实现排队叫号,多线程模拟排队叫号取号
易语言简单的多线程消息队列。@Patek。
C++多线程,消息队列用法,为了凑够20个字,拼了。
多线程中使用循环队列
实现消息队列的关键因素是考量不同线程访问消息队列的同步问题。本实现涉及到几个知识点 std::lock_guard 介绍 std::lock_gurad 是 C++11 中定义的模板类。定义如下: template <class> class lock_guard; lock_...
主要介绍了C#队列Queue多线程用法,实例分析了队列的相关使用技巧,需要的朋友可以参考下
c#实现的工作队列,workquere,多线程管理-c# realization of the work queue, workquere, multi-threaded management
多线程 队列 线程执行器 调度 生产者与消费者进行 消息队列入队出列
支持多线程的阻塞队列,使用模板技术,可存储任意类型数据
简单的多线程任务队列
多线程使用消息队列
《秒杀多线程第十六篇 多线程十大经典案例之一 双线程读写队列数据》 http://blog.csdn.net/morewindows/article/details/8646902 配套程序 在《秒杀多线程系列》的前十五篇中介绍多线程的相关概念,多线程同步互斥...
java多线程加队列上传文件_后台处理
多线程并发访问无锁队列的算法研究.pdf 多线程并发访问无锁队列的算法研究.pdf 多线程并发访问无锁队列的算法研究.pdf 多线程并发访问无锁队列的算法研究.pdf 多线程并发访问无锁队列的算法研究.pdf
java 多线程 队列工厂
该源代码使用linux的网络IO多路复用模型epoll在建立tcpserver,对用队列来缓存请求,使用线程池来处理请求。
多线程安全的消息队列模板多线程安全的消息队列模板