生产者消费者测试

#include <stdio.h>
#include <pthread.h>
#include <iostream>
using namespace std;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t event = PTHREAD_COND_INITIALIZER;


int num = 0;
int countP = 0;


bool flag=false;


void *produce(void *p)
{
    int count = 0;
    while(!flag)
    {
        pthread_mutex_lock(&lock);
        while(num==100)
        {
            cout<<"producer num==100"<<endl;
            pthread_cond_wait(&event,&lock);
        }


        bool empty = false;
        if(num==0)
        {
            empty=true;
            num++;
            cout<<"producer num==0"<<endl;
            pthread_cond_broadcast(&event);
        }

        if(!empty)

            num++;
        pthread_mutex_unlock(&lock);
        count++;
    }
    return (void*)count;
}


void *cosume(void *p)
{
    int count = 0;
    while(!flag)
    {
        pthread_mutex_lock(&lock);
        while(num==0)
        {
            cout<<"consume num==0"<<endl;
            pthread_cond_wait(&event, &lock);
        }
        countP++;
        if(countP==1000)
            flag = true;
        bool full = false;
        if(num==100)
        {
            full = true;
            num--;
            cout<<"consume num==100"<<endl;
     pthread_cond_broadcast(&event);
        }
        if(!full)

 num--;
            cout<<"consume num==100"<<endl;
     pthread_cond_broadcast(&event);
        }
        if(!full)
            num--;
        pthread_mutex_unlock(&lock);
        count++;
    }
    return (void*)count;
}


int main()
{
    pthread_t tid[4];
    pthread_create(tid+0, NULL, produce, NULL);
    sleep(1);
    pthread_create(tid+1, NULL, produce, NULL);
    sleep(1);
    pthread_create(tid+2, NULL, cosume, NULL);
    pthread_create(tid+3, NULL, cosume, NULL);


    int countP;
    int countM;
    pthread_join(tid[0], (void**)&countM);

cout<<"producer 0 "<<" "<<countM<<endl;
    pthread_join(tid[1], (void**)&countM);
    cout<<"producer 1 "<<" "<<countM<<endl;
    pthread_join(tid[2], (void**)&countM);
    cout<<"consumer 0 "<<" "<<countM<<endl;
    pthread_join(tid[3], (void**)&countM);
    cout<<"consumer 1 "<<" "<<countM<<endl;




    return 0;
}
                                                  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值