生产者消费者模型的实现

线程同步与互斥的几种实现方法:
A:mutex(互斥量)
这里写图片描述
这里写图片描述
B:. Condition Variable (条件变量)

这里写图片描述
这里写图片描述
这里写图片描述
C:Semaphore(信号量)
这里写图片描述
这里写图片描述

D: 读写锁
【实现一】:基于链表实现单线程生产者消费者模型

/*************************************************************************
  > File Name: list.c
  > Author: steve_abelieve
  > Mail: caoliang2025@foxmail.com 
  > Created Time: Sun 11 Jun 2017 05:01:14 AM PDT
 ************************************************************************/
#include<assert.h>
#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>
#include<errno.h>
//用链表建立生产者消费者模型
#include<unistd.h>
pthread_mutex_t lock=PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond=PTHREAD_COND_INITIALIZER;


typedef struct Node
{
    int data;
    struct Node* next;

}Node,* Node_p,** Node_pp;

Node_p createNode(int data)
{
    Node_p temp=(Node_p)malloc(sizeof(Node));
    if(!temp)
    {
        perror("malloc Node");
        exit(1);
    }
    temp->data=data;
    temp->next=NULL;

    return temp;
}

//链表头结点
Node_p list=NULL;
void initNode(Node_pp h)
{
    *h=createNode(0);
}


void nodeInit(Node_pp h)
{
    *h=createNode(0);
}


void pushFront(Node_p list,int data)
{
    assert(list);
    Node_p newNode=createNode(data);
    if(!newNode)
    {
        perror("Push");
        return ;
    }
    newNode->next=list->next;

    list->next=newNode;
}

void delNode(Node_p del)
{
    free(del);
}
int isEmpty(Node_p h)
{
    assert(h);
    if(h->next)
    {
        return 0;
    }
    return 1;

}

void popFront(Node_p list,int* out)
{
    //保证不能为空
    if(isEmpty(list))
    {
        printf("list Empty\n");
        return;

    }
    Node_p del=list->next;
    list->next=list->next->next;
    *out=del->data;
    delNode(del);
}
void destroy(Node_p list)
{
    int data;
    assert(list);

    while(!isEmpty(list))
    {
        popFront(list,&data);
    }
    delNode(list);
}

void ShowList(Node_p list)
{
    assert(list);
    Node_p cur=list->next;

    while(cur->next)
    {
        printf("%d->",cur->data);
        cur=cur->next;
    }
    printf("\n");
}

void* product(void*arg)
{
    int data=0;
    while(1)
    {
        int data=rand()%1234;

        sleep(1);
        //生产的时候不能去消费,加锁
        pthread_mutex_lock(&lock);
        pushFront(list,data);
        printf("prouct data done ,data is: %d\n",data);

        pthread_mutex_unlock(&lock);

        pthread_cond_signal(&cond);
    }
    return NULL;

}

void* consume(void*arg)
{
    while(1)
    {
        int data=0;

        pthread_mutex_lock(&lock);

        while(isEmpty(list))
        {
            pthread_cond_wait(&cond,&lock);
        }
        popFront(list,&data);
        printf("consume data done,data is:%d\n",data);

        pthread_mutex_unlock(&lock);
    }

    return NULL;
}

int main()
{
    pthread_t tid1;
    pthread_t tid2;
    pthread_create(&tid1,NULL,product,NULL);
    pthread_create(&tid2,NULL,consume,NULL);

    initNode(&list);



    pthread_join(tid1,NULL);
    pthread_join(tid2,NULL);
    destroy(list);
    pthread_mutex_destroy(&lock);
    pthread_cond_destroy(&cond);


    return 0;
}

【实现二】:基于环形队列实现单线程生产者消费者模型

#include<stdio.h>
#include<errno.h>
#include<pthread.h>
#include<semaphore.h>
#include<unistd.h>

#define SIZE 64
int ring[SIZE];//环形队列
sem_t blank_sem;//空白格子的信号量
sem_t data_sem;//数据的信号量

//生产数据
void* thread_product(void* arg)
{
    int i=0;
    while(1)
    {
        //sleep(1);
        sem_wait(&blank_sem);//申请格子数量,格子信号量资源-1
        //进行操作
        int data=rand()%10000;
        ring[i]=data;
        printf("Producter :%d \n",data);
        i++;
        i%=SIZE;
        sem_post(&data_sem);//释放数据资源,数据信号量资源+1
    }
    return NULL;

}


//消费数据
void * thread_consume(void* arg)
{
    int i=0;
    while(1)
    {
        sleep(1);
        sem_wait(&data_sem);//申请数据信号量资源,数据信号量资源-1//P(data)

        printf("Consume:%d\n",ring[i]);
        i++;
        i%=SIZE;
        sem_post(&blank_sem);//释放空格信号量资源,格子信号量资源+1//V(blank)
    }
    return NULL;
}


int main()
{

    sem_init(&blank_sem,0,SIZE);
    sem_init(&data_sem,0,0);

    pthread_t producter;
    pthread_t consumer;

    pthread_create(&producter,NULL,thread_product,NULL);//线程的创建
    pthread_create(&consumer,NULL,thread_consume,NULL);


    pthread_join(producter,NULL);//线程等待
    pthread_join(consumer,NULL);

    sem_destroy(&blank_sem);//信号量销毁
    sem_destroy(&data_sem);


    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值