互斥量解决生产者-消费者问题

用互斥量解决生产中-消费者问题
当生产者的生产速度低于消费者的消费速度时,消费者必须等待,所以就会产生多次不必的锁资源申请释放动作。影响系统性能。

#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>

pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;

typedef struct _Node
{
    int val;
    struct _Node* next;
}Node;

typedef struct _Args
{
    Node* head;
}Args;

Node* head;

void Init()
{
    head=NULL;
}
void PushBack(int val)
{
    Node* node=(Node*)malloc(sizeof(Node));
    node->val=val;
    node->next=NULL;

    if(head==NULL)
    {
        head=node;
        return;
    }
    Node* temp=head;
    while(temp->next!=NULL)
    {
        temp=temp->next;
    }
    temp->next=node;    
}

int PopFront()
{
    if(head==NULL)
    {
        printf("list is empty\n");
        return -1;
    }
    Node* temp=head;
    head=(head)->next;
    int res=temp->val;
    free(temp);
    return res;
}
void PrintList()
{
    Node* temp=head;
    while(temp!=NULL)
    {
        printf("%4d",temp->val);
        temp=temp->next;
    }
    printf("\n");
}
void* Consumer(void* args)
{
    for(int i=0;i<10;i++)
    {

    pthread_mutex_lock(&mutex);    
    int res=PopFront();

    if(res!=-1)
    {
         printf("In consumer:[%d]\n",res);    
    }
    pthread_mutex_unlock(&mutex);
    sleep(1);
    }
}
void* Producter(void* args)
{    
    for(int i=0;i<10;i++)
    {    
    pthread_mutex_lock(&mutex);

    PushBack(i);
    printf("In Producter:[%d]\n",i);
    pthread_mutex_unlock(&mutex);
    sleep(2);
    }
}
int main()
{
    Init(&head);
    Args args;
    args.head=head;

    pthread_t pid[2];

    pthread_create(&pid[0],NULL,Producter,(void*)&args);
    pthread_create(&pid[1],NULL,Consumer,(void*)&args);    

    pthread_join(pid[0],NULL);
    pthread_join(pid[1],NULL);

    pthread_mutex_destroy(&mutex);
    return 0;
}

结果如下
这里写图片描述



添加条件变量之后
把模型改为最多只能保留5个东西,

#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>

pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t condp=PTHREAD_COND_INITIALIZER;
pthread_cond_t condc=PTHREAD_COND_INITIALIZER;

#define MAXLEN 5

typedef struct _Node
{
    int val;
    struct _Node* next;
}Node;
typedef struct _Args
{
    Node* head;
}Args;

Node* head;
int size;

void Init()
{
    head=NULL;
    size=0;
}
void PushBack(int val)
{
    Node* node=(Node*)malloc(sizeof(Node));
    node->val=val;
    node->next=NULL;

    if(head==NULL)
    {
        head=node;
        return;
    }
    Node* temp=head;
    while(temp->next!=NULL)
    {
        temp=temp->next;
    }
    temp->next=node;
    size++;    
}
int PopFront()
{
    if(head==NULL)
    {
        printf("list is empty\n");
        return -1;
    }
    Node* temp=head;
    head=(head)->next;
    int res=temp->val;
    free(temp);
    size--;
    return res;
}
void PrintList()
{
    Node* temp=head;
    while(temp!=NULL)
    {
        printf("%4d",temp->val);
        temp=temp->next;
    }
    printf("\n");
}
void* Consumer(void* args)
{
    while(1)
    {

    pthread_mutex_lock(&mutex);
    while(size==0)
    {
         pthread_cond_wait(&condc,&mutex);
    }

   int res= PopFront();
    if(size<MAXLEN)
    {
        pthread_cond_signal(&condp);
    }

    printf("In consumer:[%d]\n",res);     

    pthread_mutex_unlock(&mutex);
    sleep(3);
    }
}
void* Producter(void* args)
{    
    while(1)
    {    
    pthread_mutex_lock(&mutex);
    while(size==MAXLEN)
    {
        pthread_cond_wait(&condp,&mutex);
    }
    int i=rand()%1000;
    PushBack(i);
    printf("size = %d\n",size);
    if(size>0)
    {
        pthread_cond_signal(&condc);
    }
    printf("In Producter:[%d]\n",i);
    pthread_mutex_unlock(&mutex);
    sleep(1);
    }
}
int main()
{
    Init(&head);
    Args args;
    args.head=head;

    pthread_t pid[2];

    pthread_create(&pid[1],NULL,Consumer,(void*)&args); 
    pthread_create(&pid[0],NULL,Producter,(void*)&args);       

    pthread_join(pid[0],NULL);
    pthread_join(pid[1],NULL);

    pthread_mutex_destroy(&mutex);
    pthread_cond_destroy(&condp);
    pthread_cond_destroy(&condc);

    return 0;
}

结果如图
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值