生产消费者模型代码

 复习使用

//=================================================================================
//                             BlockQueue.hpp
#include <iostream> 
#include <pthread.h>
#include <cstring>
#include <cstdio>
#include <unistd.h>
#include <string>
#include <signal.h>
#include <fcntl.h>
#include <queue>
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 Ty>
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<Ty> _bq;
    size_t _capacity;
    pthread_cond_t* _Full;
    pthread_cond_t* _Empty;
    pthread_mutex_t* _mtx;
};



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

void Task_2()
{
    cout<<"执行: 全盘扫描任务"<<endl;
}

void Task_3()
{
    cout<<"执行: 临时文件清除任务"<<endl;
}

void Task_4()
{
    cout<<"执行: 网络接收数据任务"<<endl;
}

typedef function<void ()> 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<func_t>*bq=(BQueue<func_t>*)ags;
    while(1)
    {
        bq->push(Tesks[rand()%4]);
        cout<<" 生产者:"<<pthread_self()<<endl;
        sleep(1);
    }
}
//************************************************
//消费者线程运行
void*consumer(void*ags)
{
    pthread_detach(pthread_self());
    BQueue<func_t>*bq=(BQueue<func_t>*)ags;
    while(1)
    {
        func_t ret;
        bq->pop(&ret);
        cout<<" 消费者:"<<pthread_self()<<" ";
        ret();
    }
}
//************************************************

int main()
{
    srand((unsigned int)time(0));
    BQueue<func_t> 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);
    
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云的小站

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值