zoukankan      html  css  js  c++  java
  • 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 
    
    
    
    
    
    
    
    
    
    
    
  • 相关阅读:
    hibernate01
    利用Struts2拦截器完成文件上传功能
    layui的CRUD案例
    最大流dinic模板 poj1273
    CodeForces
    POJ 2139 Six Degrees of Cowvin Bacon (floyd)
    POJ 3259 Wormholes (floyd或者spfa)
    POJ 3615 Cow Hurdles (flyod)
    Codeforces Round #446 (Div. 1) A. Pride
    Educational Codeforces Round 34 A. Hungry Student Problem
  • 原文地址:https://www.cnblogs.com/justinzhang/p/2296289.html
Copyright © 2011-2022 走看看