小白学linux之生产者与消费者模型实现

来源:互联网 发布:男主很帅的动漫 知乎 编辑:程序博客网 时间:2024/06/06 00:35
#include <stdio.h>#include <stdlib.h>#include <pthread.h>#include <unistd.h>pthread_mutex_t mutex;pthread_cond_t full;pthread_cond_t empty;int count=0;void* producer(void* arg){while(1){pthread_mutex_lock(&mutex);while(count>=10){pthread_cond_wait(&full,&mutex);}printf("start produce\n");++count;//printf("end produce:%d\n",count);pthread_mutex_unlock(&mutex);if(count==9){pthread_cond_signal(&full);}sleep(1);}}void* consumer(void* arg){while(1){pthread_mutex_lock(&mutex);while(count<=0){pthread_cond_wait(&full,&mutex);}//sleep(1);printf("start consume\n");--count;//printf("end consume:%d\n",count);sleep(1);pthread_mutex_unlock(&mutex);if(count==1){pthread_cond_signal(&full);}sleep(1);}}int main(int argc,const char* argv[]){pthread_t tid1,tid2,tid3,tid4; pthread_mutex_init(&mutex,NULL); pthread_cond_init(&full,NULL);pthread_cond_init(&empty,NULL); pthread_create(&tid1,NULL,producer,NULL); pthread_create(&tid2,NULL,producer,NULL);pthread_create(&tid4,NULL,consumer,NULL);pthread_create(&tid3,NULL,consumer,NULL); pthread_join(tid1,NULL);pthread_join(tid2,NULL);pthread_join(tid3,NULL);pthread_join(tid4,NULL);//sleep(2);return 0; }
也可以使用两个条件变量共享一个互斥量
<pre name="code" class="cpp">#include <stdio.h>#include <stdlib.h>#include <pthread.h>#include <unistd.h>pthread_mutex_t mutex;pthread_cond_t full;pthread_cond_t empty;int count=0;void* producer(void* arg){while(1){pthread_mutex_lock(&mutex);while(count>=10){pthread_cond_wait(&full,&mutex);}printf("start produce\n");++count;//printf("end produce:%d\n",count);pthread_mutex_unlock(&mutex);if(count==9){pthread_cond_signal(&empty);}sleep(1);}}void* consumer(void* arg){while(1){pthread_mutex_lock(&mutex);while(count<=0){pthread_cond_wait(&empty,&mutex);}//sleep(1);printf("start consume\n");--count;//printf("end consume:%d\n",count);sleep(1);pthread_mutex_unlock(&mutex);if(count==1){pthread_cond_signal(&full);}sleep(1);}}int main(int argc,const char* argv[]){pthread_t tid1,tid2,tid3,tid4; pthread_mutex_init(&mutex,NULL); pthread_cond_init(&full,NULL);pthread_cond_init(&empty,NULL); pthread_create(&tid1,NULL,producer,NULL); pthread_create(&tid2,NULL,producer,NULL);pthread_create(&tid4,NULL,consumer,NULL);pthread_create(&tid3,NULL,consumer,NULL); pthread_join(tid1,NULL);pthread_join(tid2,NULL);pthread_join(tid3,NULL);pthread_join(tid4,NULL);//sleep(2);return 0; }

还有一份代码也可以
<pre name="code" class="cpp">#include <stdio.h>#include <stdlib.h>#include <string>#include <pthread.h>#include <unistd.h>#define CELL 10#define FLOOR 0int i=0;pthread_mutex_t mutex;pthread_cond_t cond_pro,cond_con;void* pro_handler(void* arg){pthread_detach(pthread_self());while(1){pthread_mutex_lock(&mutex);while(i>=CELL){pthread_cond_wait(&cond_pro,&mutex);}i++;if(i==1){pthread_cond_signal(&cond_con);}printf("add i:%d\n",i);pthread_mutex_unlock(&mutex);sleep(rand()%5+1);}}void* con_handler(void* arg){pthread_mutex_lock(&mutex);while(1){while(i<=FLOOR){pthread_cond_wait(&cond_con,&mutex);}i--;if(i==9){pthread_cond_signal(&cond_pro);}printf("con i:%d\n",i);pthread_mutex_unlock(&mutex);sleep(rand()%5+1);}}int main(int argc,char* argv[]){int con_;pthread_t thd_con1,thd_con2,thd_pro1,thd_pro2;pthread_create(&thd_con1,NULL, con_handler,NULL);pthread_create(&thd_con2,NULL, con_handler,NULL);pthread_create(&thd_pro1,NULL, pro_handler,NULL);pthread_create(&thd_pro2,NULL, pro_handler,NULL);pthread_join(thd_con1,NULL);pthread_join(thd_con2,NULL);pthread_join(thd_pro1,NULL);pthread_join(thd_pro2,NULL);return 0;}



0 0