生产者消费者模型(条件变量和信号量分别实现)

条件变量实现:生产者消费者模型

#include<iostream>
#include<queue>
#include<pthread.h>
#include<stdio.h>

#define MAX_QUEUE 5
class BlockQueue
{
  private:
    int _capacity;//队列容量
    std::queue<int> _queue;
    pthread_mutex_t _mutex;
    pthread_cond_t _cond_pro;
    pthread_cond_t _cond_cus;
  public:
    BlockQueue(int que=MAX_QUEUE):_capacity(que)
    {
      pthread_mutex_init(&_mutex,NULL);
      pthread_cond_init(&_cond_pro,NULL);
      pthread_cond_init(&_cond_cus,NULL);

    }
    ~BlockQueue()
    {
      pthread_mutex_destroy(&_mutex);
      pthread_cond_destroy(&_cond_pro);
      pthread_cond_destroy(&_cond_cus);
    }
    
    //输入型参数加const
    bool Push(const int& data)
    {
       pthread_mutex_lock(&_mutex);
       while(_queue.size()==_capacity)
       {
         pthread_cond_wait(&_cond_pro,&_mutex);
       }
       _queue.push(data);
       pthread_cond_signal(&_cond_cus);
       pthread_mutex_unlock(&_mutex);
       return true;
    }

    //输出型参数  传地址 输入输出型 * & 不加const
    bool Pop(int* data)
    {
      pthread_mutex_lock(&_mutex);
      while(_queue.empty())
      {
        pthread_cond_wait(&_cond_cus,&_mutex);
      }
      *data=_queue.front();
      _queue.pop();
      pthread_cond_signal(&_cond_pro);
      pthread_mutex_unlock(&_mutex);
    }
};

void* productor(void* arg)
{
  BlockQueue* q=(BlockQueue*)arg;
  int data=0;
  while(1)
  {
    q->Push(data);
    printf("put data: %d\n",data++);
  }
  return NULL;
}

void* customer(void* arg)
{
  BlockQueue* q=(BlockQueue*)arg;
  while(1)
  {
    int data;
    q->Pop(&data);
    printf("get data: %d\n",data);
  }
  return NULL;
}

int main(int argc,char* argv[])
{
  BlockQueue q;
  int ret;
  pthread_t ptid[4],ctid[4];
  for(int i=0;i<4;++i)
  {
    ret = pthread_create(&ptid[i],NULL,productor,&q);
    if(ret!=0)
    {
      printf("thread creat error!\n");
      return -1;
    }
    ret = pthread_create(&ctid[i],NULL,customer,&q);
    if(ret!=0)
    {
      printf("thread creat error!\n");
      return -1;
    }
  }
  for(int i=0;i<4;++i)
  {
    pthread_join(ptid[i],NULL);
    pthread_join(ctid[i],NULL);
  }
  return 0;
}

信号量实现:生产者消费者模型

#include<iostream>
#include<vector>
#include<cstdio>
#include<semaphore.h>//信号量头文件
#include<pthread.h>

#define MAX_QUEUE 5
class RingQueue
{
  private:
    int _capacity;//队列容量
    int _step_read;//读指针
    int _step_write;//写指针
    std::vector<int> _array;
    sem_t _sem_data;//数据节点计数
    sem_t _sem_idle;//空闲节点技术
    sem_t _sem_lock;//用于实现互斥
  public:
    RingQueue(int maxq=MAX_QUEUE):_capacity(maxq),_step_read(0),_step_write(0),_array(std::vector<int>(maxq,0))
    {
     //sem_init(信号量,share标志,初值)
     sem_init(&_sem_data,0,0);
     sem_init(&_sem_idle,0,maxq);
     sem_init(&_sem_lock,0,1);
    }
    ~RingQueue()
    {
     sem_destroy(&_sem_data);
     sem_destroy(&_sem_idle);
     sem_destroy(&_sem_lock);
    }
    bool Push(const int &data)
    {
      sem_wait(&_sem_idle);//空闲节点-1
      sem_wait(&_sem_lock);//加锁
      _array[_step_write]=data;
      _step_write = (++_step_write) % _capacity;
      sem_post(&_sem_lock);//解锁
      sem_post(&_sem_data);//数据节点+1
      return true;
    }

    bool Pop(int* buf)
    {
      sem_wait(&_sem_data);
      sem_wait(&_sem_lock);
      *buf=_array[_step_read];
      _step_read=(++_step_read) % _capacity;
      sem_post(&_sem_lock);
      sem_post(&_sem_idle);
      return true;
    }
};

void* productor(void* arg)
{
  RingQueue* q=(RingQueue*)arg;
  int data=0;
  while(1)
  {
    q->Push(data);
    printf("Push data: %d\n",data++);
  }
  return NULL;
}

void* customer(void* arg)
{
  RingQueue* q=(RingQueue*)arg;
  while(1)
  {
    int data;
    q->Pop(&data);
    printf("Pop data: %d\n",data);
  }
  return NULL;
}

int main(int argc,char* argv[])
{
  RingQueue q;
  int ret;
  pthread_t ptid[4],ctid[4];
  for(int i=0;i<4;++i)
  {
    ret = pthread_create(&ptid[i],NULL,productor,&q);
    if(ret!=0)
    {
      printf("thread creat error!\n");
      return -1;
    }
    ret = pthread_create(&ctid[i],NULL,customer,&q);
    if(ret!=0)
    {
      printf("thread creat error!\n");
      return -1;
    }
  }
  for(int i=0;i<4;++i)
  {
    pthread_join(ptid[i],NULL);
    pthread_join(ctid[i],NULL);
  }
  return 0;
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值