生产消费者模型代码

 复习使用

//=================================================================================
//                             BlockQueue.hpp
#include  
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

//************************************************
//创建自动锁->自动上锁,自动释放锁
class Auto_lock
{
public:
    Auto_lock(pthread_mutex_t* reMax)
        :_AMax(reMax)
    {
        pthread_mutex_lock(_AMax);
    }
    ~Auto_lock()
    {
        pthread_mutex_unlock(_AMax);
    }

private:
    pthread_mutex_t* _AMax;
};
//************************************************
//设置等待与唤醒函数
void wait(pthread_cond_t*cond,pthread_mutex_t* mtx)
{
    pthread_cond_wait(cond,mtx);
}

void weekup(pthread_cond_t*cond)
{
    pthread_cond_signal(cond);
}
//************************************************
//建立临界资源与临界区代码
template 
class BQueue
{
    //检测bq空与满
    bool empty()
    {
        return _bq.empty();
    }
     bool full()
     {
         return _bq.size()==_capacity;
     }

public:
    BQueue(size_t capacity=5)
        : _capacity(capacity) 
    {
        //初始化条件变量
        _Full=new pthread_cond_t;
        _Empty=new pthread_cond_t;
        _mtx=new pthread_mutex_t;
        pthread_mutex_init(_mtx,nullptr);
        pthread_cond_init(_Empty,nullptr);
        pthread_cond_init(_Full,nullptr);
    }

    void push(const Ty&x)
    {
        { //放入数据
            Auto_lock am(_mtx);
            while(full()) wait(_Full,_mtx);
            _bq.push(x);
            weekup(_Empty);
        }
    }
    
    void pop(Ty*x)
    {
        {   //取数据
            Auto_lock am(_mtx);
            while(empty()) wait(_Empty,_mtx);
            *x=_bq.front();
            _bq.pop();
            weekup(_Full);
        }
    }

    ~BQueue()
    {
        pthread_mutex_destroy(_mtx);
        pthread_cond_destroy(_Empty);
        pthread_cond_destroy(_Full);
        delete _mtx;
        delete _Empty;
        delete _Full;
    }


private:
    queue _bq;
    size_t _capacity;
    pthread_cond_t* _Full;
    pthread_cond_t* _Empty;
    pthread_mutex_t* _mtx;
};



//==================================================================================
//                           teskblock.hpp
#include  
#include 
using namespace std;
void Task_1()
{
    cout<<"执行: Sql任务"< func_t;

//==================================================================================
//                               main.cc
#include"teskblock.hpp"
#include"BlockQueue.hpp"

func_t Tesks[]={Task_1,Task_2,Task_3,Task_4};
//************************************************
//生产者线程运行
void*productor(void*ags)
{
    pthread_detach(pthread_self());
    BQueue*bq=(BQueue*)ags;
    while(1)
    {
        bq->push(Tesks[rand()%4]);
        cout<<" 生产者:"<*bq=(BQueue*)ags;
    while(1)
    {
        func_t ret;
        bq->pop(&ret);
        cout<<" 消费者:"< bq(5);
    pthread_t c1,c2,p1,p2;
    pthread_create(&c1,nullptr,consumer,(void*)&bq);
    pthread_create(&c2,nullptr,consumer,(void*)&bq);
    pthread_create(&p1,nullptr,productor,(void*)&bq);
    pthread_create(&p2,nullptr,productor,(void*)&bq);

    while(1);
    
}

你可能感兴趣的:(linux,c++)