手撕的生产者与消费者模型哪里错了??拜托各位了

各位大佬们,找了一年的错误了,实在找不到了;
拜托各位大佬了,,十分感谢

//linux
  1 #include<iostream>
  2 #include<stdio.h>
  3 #include<queue>
  4 #include<pthread.h>
  5 using namespace std;
  6 #define MAX_QUEUE 5
  7 #define NUM_MEMBER 4
  8 class blockQueue{
  9 private:
 10         int _capacity;
 11         std::queue<int> _queue;
 12         pthread_mutex_t _mutex;
 13         pthread_cond_t producer_cond;
 14         pthread_cond_t consumer_cond;
 15 public:
 16         blockQueue(int cap=MAX_QUEUE)
 17         :_capacity(cap)
 18         {
 19         pthread_mutex_init(&_mutex,NULL);
 20         pthread_cond_init(&producer_cond,NULL);
 21         pthread_cond_init(&consumer_cond,NULL);
 22         }
 23 
 24         ~blockQueue()
 25         {
 26         pthread_mutex_destroy(&_mutex);
 27         pthread_cond_destroy(&producer_cond);
 28         pthread_cond_destroy(&consumer_cond);
 29         }
 30 
 31         bool Push(int data)
 32         {
 33         pthread_mutex_lock(&_mutex);
 34         while(_capacity==_queue.size()){
 35         pthread_cond_wait(&producer_cond,&_mutex);
 36         }
 37         _queue.push(data);
 38         pthread_cond_signal(&consumer_cond);
 39         pthread_mutex_unlock(&_mutex);
 40         return true;
 41 
 42         }
 43 
 44         bool Pop(int *data)
 45         {
 46         pthread_mutex_lock(&_mutex);
 47         while(_queue.empty()){
 48         pthread_cond_wait(&consumer_cond,&_mutex);
 49         }
 50         *data=_queue.front();
 51         _queue.pop();
 52         pthread_cond_signal(&producer_cond);
 53         pthread_mutex_unlock(&_mutex);
 54         return true;
 55         }
 56 };
 57 
 58 void *producer(void* arg)
 59 {
 60 blockQueue* q=(blockQueue*)arg;
 61 int i=0;
 62 while(1){
 63 q->Push(i);
 64 cout<<pthread_self()<<"push data:"<<i++<<endl;
 65 
 66 }
 67 return NULL;
 68 }
 69 void *consumer(void* arg)
 70 {
 71 blockQueue* q=(blockQueue*)arg;
 72 while(1){
 73 int data;
 74 q->Pop(&data);
 75 cout<<pthread_self()<<"get data"<<data<<endl;
 76 }
 77 return NULL;
 78 }
 79 int main()
 80 {
 81 blockQueue q;
 82 int count=NUM_MEMBER;
 83 int ret=0;
 84 pthread_t pid[NUM_MEMBER];
 85 pthread_t cid[NUM_MEMBER];
 86 int i=0;
 87 while(i<NUM_MEMBER){
 88 ret=pthread_create(&pid[i],NULL,producer,&q);{
 89 if(ret!=0){
 90 printf("创建失败\n");
 91 return -1;
 92 }
 93 }
 94 ++i;
 95 }
 96 int j=0;
 97 while(j<NUM_MEMBER){
 98 ret=pthread_create(&cid[i],NULL,consumer,&q);{
 99 if(ret!=0){
100 cout<<"创建失败"<<endl;
101 //printf("创建失败\n");
102 return -1;
103 }
104 j++;
105 }
106 int count_1;
107 while(count_1<NUM_MEMBER){
108 pthread_join(pid[i],NULL);
109 pthread_join(cid[i],NULL);
110 ++count_1;
111 }
112 }
113 return 0;
114 }


  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值