互斥量解决生产者-消费者问题
来源:互联网 发布:知轩藏书论坛 编辑:程序博客网 时间:2024/05/18 00:05
用互斥量解决生产中-消费者问题
当生产者的生产速度低于消费者的消费速度时,消费者必须等待,所以就会产生多次不必的锁资源申请释放动作。影响系统性能。
#include<pthread.h>#include<stdio.h>#include<stdlib.h>#include<unistd.h>pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;typedef struct _Node{ int val; struct _Node* next;}Node;typedef struct _Args{ Node* head;}Args;Node* head;void Init(){ head=NULL;}void PushBack(int val){ Node* node=(Node*)malloc(sizeof(Node)); node->val=val; node->next=NULL; if(head==NULL) { head=node; return; } Node* temp=head; while(temp->next!=NULL) { temp=temp->next; } temp->next=node; }int PopFront(){ if(head==NULL) { printf("list is empty\n"); return -1; } Node* temp=head; head=(head)->next; int res=temp->val; free(temp); return res;}void PrintList(){ Node* temp=head; while(temp!=NULL) { printf("%4d",temp->val); temp=temp->next; } printf("\n");}void* Consumer(void* args){ for(int i=0;i<10;i++) { pthread_mutex_lock(&mutex); int res=PopFront(); if(res!=-1) { printf("In consumer:[%d]\n",res); } pthread_mutex_unlock(&mutex); sleep(1); }}void* Producter(void* args){ for(int i=0;i<10;i++) { pthread_mutex_lock(&mutex); PushBack(i); printf("In Producter:[%d]\n",i); pthread_mutex_unlock(&mutex); sleep(2); }}int main(){ Init(&head); Args args; args.head=head; pthread_t pid[2]; pthread_create(&pid[0],NULL,Producter,(void*)&args); pthread_create(&pid[1],NULL,Consumer,(void*)&args); pthread_join(pid[0],NULL); pthread_join(pid[1],NULL); pthread_mutex_destroy(&mutex); return 0;}
结果如下
添加条件变量之后
把模型改为最多只能保留5个东西,
#include<pthread.h>#include<stdio.h>#include<stdlib.h>#include<unistd.h>pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;pthread_cond_t condp=PTHREAD_COND_INITIALIZER;pthread_cond_t condc=PTHREAD_COND_INITIALIZER;#define MAXLEN 5typedef struct _Node{ int val; struct _Node* next;}Node;typedef struct _Args{ Node* head;}Args;Node* head;int size;void Init(){ head=NULL; size=0;}void PushBack(int val){ Node* node=(Node*)malloc(sizeof(Node)); node->val=val; node->next=NULL; if(head==NULL) { head=node; return; } Node* temp=head; while(temp->next!=NULL) { temp=temp->next; } temp->next=node; size++; }int PopFront(){ if(head==NULL) { printf("list is empty\n"); return -1; } Node* temp=head; head=(head)->next; int res=temp->val; free(temp); size--; return res;}void PrintList(){ Node* temp=head; while(temp!=NULL) { printf("%4d",temp->val); temp=temp->next; } printf("\n");}void* Consumer(void* args){ while(1) { pthread_mutex_lock(&mutex); while(size==0) { pthread_cond_wait(&condc,&mutex); } int res= PopFront(); if(size<MAXLEN) { pthread_cond_signal(&condp); } printf("In consumer:[%d]\n",res); pthread_mutex_unlock(&mutex); sleep(3); }}void* Producter(void* args){ while(1) { pthread_mutex_lock(&mutex); while(size==MAXLEN) { pthread_cond_wait(&condp,&mutex); } int i=rand()%1000; PushBack(i); printf("size = %d\n",size); if(size>0) { pthread_cond_signal(&condc); } printf("In Producter:[%d]\n",i); pthread_mutex_unlock(&mutex); sleep(1); }}int main(){ Init(&head); Args args; args.head=head; pthread_t pid[2]; pthread_create(&pid[1],NULL,Consumer,(void*)&args); pthread_create(&pid[0],NULL,Producter,(void*)&args); pthread_join(pid[0],NULL); pthread_join(pid[1],NULL); pthread_mutex_destroy(&mutex); pthread_cond_destroy(&condp); pthread_cond_destroy(&condc); return 0;}
结果如图
阅读全文
0 0
- 互斥量解决生产者-消费者问题
- 信号量解决生产者,消费者问题
- 信号量解决生产者-消费者问题
- 【转】linux C 解决 生产者消费者问题
- 1,使用信号量解决生产者-消费者问题
- 使用POSIX线程解决“生产者/消费者”问题
- 用线程解决生产者消费者问题
- 用信号量解决生产者消费者问题
- 使用互斥锁解决生产者与消费者问题
- 用信号量解决生产者消费者问题
- Java多线程解决生产者消费者问题
- 记录型信号量解决消费者-生产者问题
- 生产者和消费者经典问题OC解决
- NSCondition 多线程解决生产者消费者问题
- 信号量机制解决生产者--消费者问题
- 多线程解决生产者与消费者问题
- 生产者消费者模式解决强耦合问题
- 互斥锁加条件变量解决生产者消费者问题
- java的位运算与移位运算实例研究
- Drying POJ
- Spring4.X使用c3p0加载配置文件连接数据库,出现的Access denied for user 'root'@'localhost' 错误!
- Django配置表单
- 一次面试
- 互斥量解决生产者-消费者问题
- solr学习(2.1)-solr 4.7.2 环境搭建
- Nginx服务搭建负载均衡,反向代理,缓存加速,访问分布式文件系统高可用
- Python-Every-Day:0001——优惠券(coupon)
- 剑指offer:斐波那契数列
- 如何理解vue的slot
- Java垃圾回收过程
- [3]Python学习笔记---写一个Python程序把CSDN专栏的所有文章另存为本地网页
- ios中屏幕触摸和摇晃的方法