c++实现生产则消费者模式

12 篇文章 0 订阅
4 篇文章 0 订阅

简单的生产者消费者模式,缓冲区采用stack,当然你也可以使用list circle-buffer等数据结构;
读写数据时要加锁,实现线程的同步,这里采用条件变量来处理读写动作,先锁定互斥变量,然后调用wait循环检查stack条件(写入时检查是否已满,读取时检查是否为空),直到满足条件,然后通知其它线程。

代码如下:

#include<stack>
#include<mutex>
#include<thread>

using namespace std;


class CBuffer
{
public:
    explicit CBuffer(int iCapacity = 0);
    ~CBuffer();
public:
    void PutData();
    void GetData();
    void put(int x);
    void get(int* x);
    bool bEmpty();
    bool bFull();
private:
    mutex m_mtx;
    condition_variable m_condition_put;
    condition_variable m_condition_get;
    int m_iUnRead;
    int m_iCapacity;
    stack<int> m_stack;
}

//.cpp
#include"ConditionVariable.h"

CBuffer::CBuffer(int iCapacity)
	:m_iCapacity(iCapacity)
{
}
CBuffer::~CBuffer()
{
}
void CBuffer::PutData()
{
for(int i = 0; i < 10;i++)
{
   cout<<"put:"<<i<<endl;
   put(i);
}
}
void CBuffer::GetData()
{
   int iData;
   for(int i = 0; i < 10;i++)
    {   
       get(&iData);  
       cout<<"get:"<<iData<<endl;
    }
}
void CBuffer::put(int x)
{
    unique_lock<mutex> lock(m_mtx);
    for(;bFull();)
    {
        cout<<"buffer is full:"<<endl;
       //需要等待线程,直到条件满足,然后写数据
        m_condition_put.wait(lock);
    }
    m_stack.push(x);
    ++m_iUnRead;
    lock.unlock();
    //通知读数据的线程,可以读取数据了
    m_condition_get.notify_one();
}
void CBuffer::get(int* x)
{
    unique_lock<mutex> lock(m_mtx);
    for(;bEmpty();)
    {
        cout<<"buffer is empty:"<<endl;
       //需要等待线程,直到条件满足,然后读数据
        m_condition_get.wait(lock);
    }
    *x = m_stack.top();
    m_stack.pop();
    m_iUnRead--;
    //通知写数据的线程,可以写数据了
    m_condition_put.notify_one();
}
bool CBuffer::bFull()
{
    return m_iUnRead == m_iCapacity;
}
bool CBuffer::bEmpty()
{
    return m_iUnRead == 0;
}

void main()
{
    CBuffer buffer(5);
    thread thread_put(&CBuffer::PutData,&buffer); //可参考c++11中thread构造函数的用法
    thread thread_get1(&CBuffer::GetData,&buffer);
    thread thread_get2(&CBuffer::GetData,&buffer);
    thread_put.join();                            //join用于阻塞线程,直到线程结束
    thread_get1.join();
    thread_get2.join();
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值