本文主要是介绍基于环形队列的多生产多消费模型,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Linux中信号量相关接口及环形队列-CSDN博客https://blog.csdn.net/2301_77479435/article/details/141411372
main.cc
#include"RingQueue.hpp"
#include"Task.hpp"
#include<pthread.h>
#include<ctime>
#include<cstdlib>
#include<sys/types.h>
#include<unistd.h>string SelfName()
{char name[128];snprintf(name,sizeof name,"thread[0x%x]",pthread_self());return name;
}//生产者线程和消费者线程那个限制性无法确定
void* ProductorRoutine(void* rq)
{RingQueue<Task>*ringqueue =static_cast<RingQueue<Task>* >(rq);while(true){//version1// int data=rand()%10+1;// ringqueue->Push(data);// cout<<"生产完成,生产的数据为: "<<data<<endl;//version2//构建任务int x=rand()%10;int y=rand()%5;char op=oper[rand()%oper.size()];Task t(x,y,op,mymath);//生产任务ringqueue->Push(t);//输出提示cout<<SelfName()<<", 生产者派发了一个任务: "<<t.toTaskString()<<endl;sleep(1);}
}
void* ConsumerRoutine(void* rq)
{RingQueue<Task>* ringqueue=static_cast<RingQueue<Task>* >(rq);while(true){//version1// int data;// ringqueue->Pop(&data);// cout<<"消费完成, 消费的数据为: "<<data<<endl;Task t;ringqueue->Pop(&t);string result=t();cout<<SelfName()<<", 消费者消费了一个任务: "<<result<<endl;}
}
int main()
{srand((unsigned int)time(nullptr));RingQueue<Task>* rq=new RingQueue<Task>();//单生产,单消费,多生产,多消费 --> 只要保证,最终进入临界区的是一个生产,一个消费就行!!!pthread_t p[4],c[8];for(int i=0;i<4;i++)pthread_create(p+i,nullptr,ProductorRoutine,rq);for(int i=0;i<8;i++)pthread_create(c+i,nullptr,ConsumerRoutine,rq);for(int i=0;i<4;i++)pthread_join(p[i],nullptr);for(int i=0;i<8;i++)pthread_join(c[i],nullptr);delete rq;return 0;
}
Ringqueue.hpp
#pragma once#include<cassert>
#include<iostream>
#include<vector>
#include<semaphore.h>
#include<pthread.h>static const int gcap=5;
using namespace std;
template<class T>
class RingQueue
{void P(sem_t &sem){int n=sem_wait(&sem);assert(n==0);}void V(sem_t &sem){int n=sem_post(&sem);assert(n==0);}
public:RingQueue(const int& cap=gcap):_queue(cap),_cap(cap){int n=sem_init(&_spaceSem,0,_cap);assert(n==0);n=sem_init(&_dataSem,0,0);assert(n==0);_productorStep=_consumerStep=0;pthread_mutex_init(&_pmutex,nullptr);pthread_mutex_init(&_cmutex,nullptr);}//优化:提高信号量的并发度void Push(const T &in){P(_spaceSem);//申请到了空间信号量,意味着能进行正常生产pthread_mutex_lock(&_pmutex);//生产者生产时因保证环形队列不能为满_queue[_productorStep++]=in;_productorStep%=_cap; pthread_mutex_unlock(&_pmutex);V(_dataSem);}void Pop(T* out){ P(_dataSem);pthread_mutex_lock(&_cmutex); *out=_queue[_consumerStep++];_consumerStep%=_cap; pthread_mutex_unlock(&_cmutex);V(_spaceSem);}~RingQueue(){sem_destroy(&_spaceSem);sem_destroy(&_dataSem);pthread_mutex_destroy(&_pmutex);pthread_mutex_destroy(&_cmutex);}
private:vector<T> _queue;int _cap;//队列容量sem_t _spaceSem;//生产者看重空间资源sem_t _dataSem;//消费者看重数据资源int _productorStep;int _consumerStep;pthread_mutex_t _pmutex;pthread_mutex_t _cmutex;
};
Task.hpp
#pragma once#include<cstdio>
#include<iostream>
#include<string>
#include<functional>using namespace std;
class Task
{using func_t=function<int(int,int,char)>;//typedef function<int(int,int)> func_t;
public:Task(){}Task(int x,int y,char op,func_t func):_x(x),_y(y),_op(op),_callback(func){}string operator()(){int result=_callback(_x,_y,_op);//构建结构字符串char buffer[1024];snprintf(buffer,sizeof buffer,"%d %c %d = %d ",_x,_op,_y,result);return buffer;}string toTaskString(){char buffer[1024];snprintf(buffer,sizeof buffer,"%d %c %d = ? ",_x,_op,_y);return buffer;}
private:int _x;int _y;char _op;func_t _callback;
};
const string oper="+-*/";
int mymath(int x,int y,char op)
{int result=0;switch(op){case '+':result=x+y;break;case '-':result=x-y;break;case '*':result=x*y;break;case '/':{if(y==0){cerr<<"div zero error!!!"<<endl;result=-1;}elseresult=x/y;}break;case '%':{if(y==0){cerr<<"mod zero error!!!"<<endl;result=-1;}elseresult=x%y;} break;default:break;}return result;
}
class SaveTask
{typedef function<void(const string&)> func_t;
public:SaveTask(){}SaveTask(const string &message,func_t func):_message(message),_func(func){}void operator()(){_func(_message);}
private:string _message;func_t _func;
};
void Save(const string& message)
{const string target="./log.txt";FILE* fp=fopen(target.c_str(),"a+");if(!fp){cerr<<" fopen error "<<endl;return;} fputs(message.c_str(),fp);fputs("\n",fp);fclose(fp);
}
这篇关于基于环形队列的多生产多消费模型的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!