多线程(生产者和消费者模型)使用条件变量和互斥锁

生产者和消费者线程各创建5个。

商品队列使用的是带头结点链表,插入方式是头插法。

条件变量只创建1个,用于判断商品是否已空。因为使用链表保存,所以不用判断是否已满。

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

//生产者消费者模型

//商品结点
struct node
{
    int num;
    struct node *next; 
};

//声明一个指向链表的指针
struct node * head = NULL;  

//创建一个互斥锁和条件变量
pthread_mutex_t mutex;
pthread_cond_t cond;


void * product(void *arg)
{
    struct node *newnode = NULL;

    while (1)
    {
        pthread_mutex_lock(&mutex);

        newnode = (struct node *)malloc(sizeof(struct node));
        newnode->num = rand()%100;

        newnode->next = head->next;
        head->next = newnode;

        printf("生产者, id:%ld num:%d\n",pthread_self(),newnode->num);

        pthread_mutex_unlock(&mutex);

        pthread_cond_signal(&cond);

        sleep(rand()%3);
    }

    return NULL;
}

void * consume(void *arg)
{   
    struct node *newnode = NULL;

    while(1)
    {
        pthread_mutex_lock(&mutex);

        while(head->next == NULL)
        {
            pthread_cond_wait(&cond,&mutex);
        }

        newnode = head->next;

        printf("消费者, id:%ld num:%d\n",pthread_self(),newnode->num);

        head->next = newnode->next;

        free(newnode);

        newnode = NULL;

        pthread_mutex_unlock(&mutex);

        sleep(rand()%3);
    }

    return NULL;
}

int main()
{
    //创建链表头结点
    head = (struct node *)malloc(sizeof(struct node));
    head->next = NULL;

    //初始化 锁和条件变量
    pthread_mutex_init(&mutex,NULL);
    pthread_cond_init(&cond,NULL);

    srand((unsigned)time(NULL));

    pthread_t t1[5],t2[5];     //t1 生产者,t2 消费者

    //创建5个生产者线程
    for(int i = 0; i < 5; i++)
    {
        pthread_create(&(t1[i]),NULL,product,NULL);
    }

     //创建5个消费者线程
    for(int i = 0; i < 5; i++)
    {
        pthread_create(&(t2[i]),NULL,consume,NULL);
    }

    //线程等待
    for(int i = 0; i < 5; i++)
    {
        pthread_join(t1[i],NULL);
        pthread_join(t2[i],NULL);
    }

    //销毁锁和条件变量
    pthread_mutex_destroy(&mutex);
    pthread_cond_destroy(&cond);

    return 0;
}

注意:这里条件变量的判断不能使用if,if只判断最初的一次,条件变量结束等待之后就不做判断了。但是消费者有5个。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值