POSIX生产者、消费者模式

21 篇文章 0 订阅

1、生产者、消费者

 

 

02.c

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

//产品队列
int ready = 0;

//互斥锁
pthread_mutex_t mutex;

//条件变量
pthread_cond_t has_product;

//生产
void* producer(void* arg){
        int no = (int)arg;
        //条件变量

        for(;;){

                pthread_mutex_lock(&mutex);
                //往队列中添加产品
                ready++;
                printf("producer %d, produce product \n",no);

                //通知消费者,有新的产品可以消费了
                pthread_cond_signal(&has_product);
                printf("producer %d,signal",no);
                pthread_mutex_unlock(&mutex);
                sleep(1);

        }

}

//消费者
void* consumer(void* arg){
  for(;;){
        pthread_mutex_lock(&mutex);
        while(ready==0){
                //没有产品,继续等待
                //1、阻塞等待has_product被唤醒
                //2、释放互斥锁,thread_mutex_unlock
                //3、被唤醒时,解除阻塞,重新申请获得互斥锁pthread_mutex_lock
                pthread_cond_wait(&has_product,&mutex);
        }

        //有产品,消费产品
        ready--;
        printf("consume product \n");

        pthread_mutex_unlock(&mutex);
        sleep(1);
  }
}



void main(){
        pthread_t tid_p;
        pthread_t tid_c;
        //初始化互斥锁和条件变量
        pthread_mutex_init(&mutex,NULL);
        pthread_cond_init(&has_product,NULL);
        //生产者线程
        pthread_create(&tid_p,NULL,producer,NULL);

        //消费者线程
        pthread_create(&tid_c,NULL,consumer,NULL);

        //等待
        pthread_join(tid_p,NULL);
        pthread_join(tid_c,NULL);

        pthread_mutex_destroy(&mutex);
        pthread_cond_destroy(&has_product);
}

 

 

运行后生产者生产产品后消费者进行消费:

 

 

2、单生产者多消费者模型

线程创建:

 

生产者通知消费者:

消费者消费产品:

02.c

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

//消费者数量
#define CONSUMER_NUM 2
//生产者数量
#define PRODUCER_NUM 1

pthread_t pids[CONSUMER_NUM+PRODUCER_NUM];

//产品队列
int ready = 0;

//互斥锁
pthread_mutex_t mutex;
          
//条件变量
pthread_cond_t has_product;
         
//生产  
void* producer(void* arg){
        int no = (int)arg;
        //条件变量
        for(;;){
                
                pthread_mutex_lock(&mutex);
                //往队列中添加产品
                ready++;
                printf("producer %d, produce product \n",no);
                
                //通知消费者,有新的产品可以消费了
                pthread_cond_signal(&has_product);
                printf("producer %d,signal \n",no);
                pthread_mutex_unlock(&mutex);
                sleep(1);

        }

}

//消费者
void* consumer(void* arg){
  int num = (int)arg;

  for(;;){
        pthread_mutex_lock(&mutex);
        while(ready==0){
                //没有产品,继续等待
                //1、阻塞等待has_product被唤醒
                //2、释放互斥锁,thread_mutex_unlock
                //3、被唤醒时,解除阻塞,重新申请获得互斥锁pthread_mutex_lock
                printf("%d consumer wait \n");
                pthread_cond_wait(&has_product,&mutex);

        }

        //有产品,消费产品
        ready--;
        printf("%d consume product \n",num);

        pthread_mutex_unlock(&mutex);
        sleep(1);
  }
}



void main(){
        //初始化互斥锁和条件变量
        pthread_mutex_init(&mutex,NULL);
        pthread_cond_init(&has_product,NULL);

        int i;
        for(i=0;i<PRODUCER_NUM;i++){
                //生产者线程
                pthread_create(&pids[i],NULL,producer,(void*)i);
        }


        //消费者线程
        for(i=0;i<CONSUMER_NUM;i++){
                //消费者线程
                pthread_create(&pids[PRODUCER_NUM+i],NULL,consumer,(void*)i);
        }



        //等待
        sleep(10);
        for(i=0;i<CONSUMER_NUM+PRODUCER_NUM;i++){
                pthread_join(pids[i],NULL);
        }

        //销毁互斥锁和条件变量
        pthread_mutex_destroy(&mutex);
        pthread_cond_destroy(&has_product);
}

运行结果:

一个线程生产,多个线程消费

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值