linux进程线程下简单的消费者和生产者
2017-04-27 21:44
295 查看
在大家学习进程或者线程的时候肯定都会遇到生产者和消费者问题,也称为有界缓冲区。生产者消费者和现实中类似,只是在编程中一切都是数据而已,生产者和消费者简单的有一对一的关系,复杂的就有一对多、多对一、多对多的关系,要注意的是生产者和消费者都必须是在操作一块内存或者文件。在这里我仅为了熟练线程操作,使用线程来完成,并选则简单的增减数字来模拟生产者消费者关系,为了保证每一个线程可以执行完成在这里应用了互斥锁pthread_mutex_lock(pthread_mutex_t mutex)和等待唤醒pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t mutex)、pthread_cond_signal(pthread_cond_t* cond)函数,详细使用查阅man手册3;下面我们来看看图解和代码实现。
代码:
代码:
在最后也说一下本人再学习线程中遇到的一个疑问就是创建简单的多线的时候,线程的创建先后顺序会乱,这是因为现在的电脑都是四核四线或者四核八线的配置,在程序执行的时候短时间会把每个线程的创建分配给每个内核的线程同时执行,由于不一定每个内核的线程都是空闲的所以会出现创建顺序的先好颠倒现象,并不是我们想象的线程有缓存区。
1、一对一的生产者和消费者:
代码:
#include<iostream> #include<pthread.h> #include<unistd.h> using namespace std; int count = 0; pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t cond = PTHREAD_COND_INITIALIZER; pthread_cond_t cond1 = PTHREAD_COND_INITIALIZER; void *Product(void *arg) { //pthread_mutex_lock(&mutex); while(1) { pthread_mutex_lock(&mutex); //int index = *(int *)arg; if(count >= 500) { pthread_cond_signal(&cond1); pthread_cond_wait(&cond, &mutex); } ++count; cout<<"This is product:> "<<count<<endl; pthread_mutex_unlock(&mutex); } } void *Consumer(void *arg) { while(1) { pthread_mutex_lock(&mutex); if(count <= 0) { pthread_cond_signal(&cond); pthread_cond_wait(&cond1,&mutex); } --count; cout<<"This is Consumer:> "<<count<<endl; pthread_mutex_unlock(&mutex); } } int main() { pthread_t tid[2]; while(1) { pthread_create(&tid[0],NULL,Product,NULL); pthread_create(&tid[1],NULL,Consumer,NULL); pthread_join(tid[0], NULL); pthread_join(tid[1], NULL); break; } return 0; }
2、一对多的生产者和消费者们:
#include<iostream> #include<unistd.h> #include<pthread.h> using namespace std; int count = 0; pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t cond = PTHREAD_COND_INITIALIZER; pthread_cond_t cond1 = PTHREAD_COND_INITIALIZER; void *Product(void *arg) { while(1) { pthread_mutex_lock(&mutex); if(count >= 15) { pthread_cond_signal(&cond1); pthread_cond_wait(&cond, &mutex); } ++count; cout<<"This is product:> "<<count<<endl; pthread_mutex_unlock(&mutex); } } void *Consumer1(void *arg) { while(1) { pthread_mutex_lock(&mutex); if(count <= 0) { pthread_cond_signal(&cond); pthread_cond_wait(&cond1,&mutex); } --count; cout<<"This is Consumer1:> "<<count<<endl; pthread_mutex_unlock(&mutex); } } void *Consumer2(void *arg) { while(1) { pthread_mutex_lock(&mutex); if(count <= 0) { pthread_cond_signal(&cond); pthread_cond_wait(&cond1,&mutex); } --count; cout<<"This is Consumer2:> "<<count<<endl; pthread_mutex_unlock(&mutex); } } void *Consumer3(void *arg) { while(1) { pthread_mutex_lock(&mutex); if(count <= 0) { pthread_cond_signal(&cond); pthread_cond_wait(&cond1,&mutex); } --count; cout<<"This is Consumer3:> "<<count<<endl; pthread_mutex_unlock(&mutex); } } void *Consumer4(void *arg) { while(1) { pthread_mutex_lock(&mutex); if(count <= 0) { pthread_cond_signal(&cond); pthread_cond_wait(&cond1,&mutex); } --count; cout<<"This is Consumer4:> "<<count<<endl; pthread_mutex_unlock(&mutex); } } int main() { pthread_t tid[5]; while(1) { pthread_create(&tid[0],NULL,Product,NULL); pthread_create(&tid[1],NULL,Consumer1,NULL); pthread_create(&tid[2],NULL,Consumer2,NULL); pthread_create(&tid[3],NULL,Consumer3,NULL); pthread_create(&tid[4],NULL,Consumer4,NULL); for(int i=0; i<5; ++i) { pthread_join(tid[i], NULL); } } return 0; }
3、多对一的生产者们和消费者:
代码:
#include<iostream> #include<unistd.h> #include<pthread.h> using namespace std; int count = 0; pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t cond = PTHREAD_COND_INITIALIZER; pthread_cond_t cond1 = PTHREAD_COND_INITIALIZER; void *Product1(void *arg) { while(1) { pthread_mutex_lock(&mutex); if(count >= 15) { pthread_cond_signal(&cond1); pthread_cond_wait(&cond, &mutex); } ++count; cout<<"This is product1:> "<<count<<endl; pthread_mutex_unlock(&mutex); } } void *Product2(void *arg) { while(1) { pthread_mutex_lock(&mutex); if(count >= 15) { pthread_cond_signal(&cond1); pthread_cond_wait(&cond, &mutex); } ++count; cout<<"This is product2:> "<<count<<endl; pthread_mutex_unlock(&mutex); } } void *Product3(void *arg) { while(1) { pthread_mutex_lock(&mutex); if(count >= 15) { pthread_cond_signal(&cond1); pthread_cond_wait(&cond, &mutex); } ++count; cout<<"This is product3:> "<<count<<endl; pthread_mutex_unlock(&mutex); } } void *Consumer(void *arg) { while(1) { pthread_mutex_lock(&mutex); if(count <= 0) { pthread_cond_signal(&cond); pthread_cond_wait(&cond1,&mutex); } --count; cout<<"This is Consumer:> "<<count<<endl; pthread_mutex_unlock(&mutex); } } int main() { pthread_t tid[4]; while(1) { pthread_create(&tid[0],NULL,Product1,NULL); pthread_create(&tid[1],NULL,Product2,NULL); pthread_create(&tid[2],NULL,Product3,NULL); pthread_create(&tid[3],NULL,Consumer,NULL); for(int i=0; i<4; ++i) { pthread_join(tid[i], NULL); } } return 0; }
4、多对多的生产者和消费者们:
#include<iostream> #include<unistd.h> #include<pthread.h> using namespace std; int count = 0; pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t cond = PTHREAD_COND_INITIALIZER; pthread_cond_t cond1 = PTHREAD_COND_INITIALIZER; void *Product1(void *arg) { while(1) { pthread_mutex_lock(&mutex); if(count >= 15) { pthread_cond_signal(&cond1); pthread_cond_wait(&cond, &mutex); } ++count; cout<<"This is product1:> "<<count<<endl; pthread_mutex_unlock(&mutex); } } void *Product2(void *arg) { while(1) { pthread_mutex_lock(&mutex); if(count >= 15) { pthread_cond_signal(&cond1); pthread_cond_wait(&cond, &mutex); } ++count; cout<<"This is product2:> "<<count<<endl; pthread_mutex_unlock(&mutex); } } void *Product3(void *arg) { while(1) { pthread_mutex_lock(&mutex); if(count >= 15) { pthread_cond_signal(&cond1); pthread_cond_wait(&cond, &mutex); } ++count; cout<<"This is product3:> "<<count<<endl; pthread_mutex_unlock(&mutex); } } void *Consumer1(void *arg) { while(1) { pthread_mutex_lock(&mutex); if(count <= 0) { pthread_cond_signal(&cond); pthread_cond_wait(&cond1,&mutex); } --count; cout<<"This is Consumer1:> "<<count<<endl; pthread_mutex_unlock(&mutex); } } void *Consumer2(void *arg) { while(1) { pthread_mutex_lock(&mutex); if(count <= 0) { pthread_cond_signal(&cond); pthread_cond_wait(&cond1,&mutex); } --count; cout<<"This is Consumer2:> "<<count<<endl; pthread_mutex_unlock(&mutex); } } void *Consumer3(void *arg) { while(1) { pthread_mutex_lock(&mutex); if(count <= 0) { pthread_cond_signal(&cond); pthread_cond_wait(&cond1,&mutex); } --count; cout<<"This is Consumer3:> "<<count<<endl; pthread_mutex_unlock(&mutex); } } void *Consumer4(void *arg) { while(1) { pthread_mutex_lock(&mutex); if(count <= 0) { pthread_cond_signal(&cond); pthread_cond_wait(&cond1,&mutex); } --count; cout<<"This is Consumer4:> "<<count<<endl; pthread_mutex_unlock(&mutex); } } int main() { pthread_t tid[7]; while(1) { pthread_create(&tid[0],NULL,Product1,NULL); pthread_create(&tid[1],NULL,Product2,NULL); pthread_create(&tid[2],NULL,Product3,NULL); pthread_create(&tid[3],NULL,Consumer1,NULL); pthread_create(&tid[4],NULL,Consumer2,NULL); pthread_create(&tid[5],NULL,Consumer3,NULL); pthread_create(&tid[6],NULL,Consumer4,NULL); for(int i=0; i<7; ++i) { pthread_join(tid[i], NULL); } } return 0; }
在最后也说一下本人再学习线程中遇到的一个疑问就是创建简单的多线的时候,线程的创建先后顺序会乱,这是因为现在的电脑都是四核四线或者四核八线的配置,在程序执行的时候短时间会把每个线程的创建分配给每个内核的线程同时执行,由于不一定每个内核的线程都是空闲的所以会出现创建顺序的先好颠倒现象,并不是我们想象的线程有缓存区。
相关文章推荐
- 以生产者消费者为例实现Linux进程线程控制
- Linux进程间的通信,信号量的使用,生产者消费者
- linux下c++实现简单的生产者消费者队列模式
- 进程与线程,同步与死锁,生产者消费者模式
- Java线程(学习整理)--4---一个简单的生产者、消费者模型
- Linux下信号量实现进程同步、互斥(生产者消费者问题)
- Linux进程间的通信,信号量的使用,生产者消费者
- 进程、线程、生产者消费者问题
- Operating System-进程/线程内部通信-信号量、PV操作的实现和应用(解决哲学家进餐和生产者消费者问题)
- 今天在学习线程的时候接触到消费者和生产者模式,写了简单的代码
- Java- 简单了解线程 生产者与消费者问题(三)
- Linux下进程的同步互斥实例——生产者消费者
- Linux(2)-----进程与线程的一个简单解释
- 谈谈我对Linux下“生产者/消费者线程模型”的理解
- Linux下用fork()派生的子进程通过pipe管道通讯的实例详解("生产者-消费者"问题)
- windows下(互斥量和事件对象)实现简单象消费者和生产者线程
- Linux下进程的同步互斥实例——生产者消费者
- Linux下进程的同步互斥实例——生产者消费者
- 多线程实现生产者消费者模型,以及线程和进程的回顾
- 进程与线程IPC-信号量(实现生产者消费者&哲学家就餐)