• Linux下生产者与消费者问题


    #include <stdio.h>
    #include <pthread.h>
    #define MAX 10 //需要生产的数量
    pthread_mutex_t the_mutex;
    pthread_cond_t condc, condp;
    int buffer = 0;//生产者、消费者使用的缓冲区
    
    void *producer(void *ptr)
    {
        int i;
        for(i=1; i<=MAX; i++)
        {
            pthread_mutex_lock(&the_mutex); //互斥使用缓冲区
            while(buffer !=0) pthread_cond_wait(&condp, &the_mutex);
            printf("procucer produce item %d\n",i);
            buffer = i; //将数据插入缓冲区
            pthread_cond_signal(&condc);//唤醒消费者
            pthread_mutex_unlock(&the_mutex);//释放缓冲区
        }
        
        pthread_exit(0);
    
    }
    
    void *consumer(void *ptr)
    {
    
        int i;
        for(i=1; i<=MAX; i++)
        {
            pthread_mutex_lock(&the_mutex);//互斥使用缓冲区
            while(buffer ==0) pthread_cond_wait(&condc, &the_mutex);
            printf("consumer consume item %d\n",i);
            buffer = 0;//从缓冲区中取出数据
            pthread_cond_signal(&condp);//唤醒生产者
            pthread_mutex_unlock(&the_mutex);//释放缓冲区
        }
        pthread_exit(0);
    
    }
    
    int main(int argc, char *argv[])
    {
        pthread_t pro, con;
        pthread_mutex_init(&the_mutex, 0);
        pthread_cond_init(&condc,0);
        pthread_cond_init(&condp,0);
        pthread_create(&con, 0, consumer, 0);
        pthread_create(&pro, 0, producer, 0);
        pthread_join(pro,0);
        pthread_join(con,0);
        pthread_cond_destroy(&condc);
        pthread_cond_destroy(&condp);
        pthread_mutex_destroy(&the_mutex);
        return 0;    
    }
    

    gcc -o consumer-producer -lpthread consumer-producer.c

    运行结果:
     
    image 
    
    
    
    
    
    
    
    
    
    
    
  • 相关阅读:
    UVa 10213
    树莓派学习路程No.2 GPIO功能初识 wiringPi安装
    树莓派学习路程No.1 树莓派系统安装与登录 更换软件源 配置wifi
    《软件工程》 的课程总结附加题。
    软件工程《个人总结》
    Java super关键字活用
    软件工程:黄金G点小游戏1.0
    Android之获取数据库路径
    Android自定义折线图
    软件工程:vs单元测试
  • 原文地址:https://www.cnblogs.com/justinzhang/p/2296289.html
Copyright © 2020-2023  润新知