生产者消费者模型
来源:互联网 发布:java开源聊天机器人 编辑:程序博客网 时间:2024/06/04 19:06
生产者与消费者:
3,2,1
三种关系:
生产者与消费者:互斥,同步
消费者与消费者:互斥
生产者与生产者:互斥
条件变量:
int pthread_cond_destroy(pthread_cond_t *cond);
int pthread_cond_init(pthread_cond_t *restrict cond,
const pthread_condattr_t *restrict attr);
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
返回值:
成功返回0,失败返回错误号。
堵塞等待:
int pthread_cond_wait(pthread_cond_t *restrict cond,
pthread_mutex_t *restrict mutex);
唤醒:
int pthread_cond_broadcast(pthread_cond_t *cond);
int pthread_cond_signal(pthread_cond_t *cond);
一个Condition Variable总是和一个Mutex搭配使用的。
一个线程可以调用pthread_cond_wait在一个Condition Variable上阻塞等待,这个函数做以下三步操作:
1. 释放Mutex
2. 阻塞等待
3. 当被唤醒时,重新获得Mutex并返回
先进后出: 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<pthread.h> 4 5 typedef int _datatype; 6 typedef struct _list 7 { 8 _datatype _val; 9 struct _list* _next; 10 }list; 11 12 13 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; 14 pthread_cond_t cond = PTHREAD_COND_INITIALIZER; 15 list* head=NULL; 16 17 18 list* Buy_node() 19 { 20 list* cur=NULL; 21 cur=(list*)malloc(sizeof(list)); 22 return cur; 23 } 24 void Init_list(list** _head) 25 { 26 list* cur=*_head; 27 28 if(cur==NULL) 29 { 30 cur=Buy_node(); 31 cur->_val=0; 32 cur->_next=NULL; 33 } 34 *_head=cur; 35 36 } 37 void Push_node(list* head,_datatype val) 38 { 39 list* cur=head->_next; 40 if(cur==NULL) 41 { 42 cur=Buy_node(); 43 cur->_val=val; 44 cur->_next=NULL; 45 head->_next=cur; 46 } 47 else 48 { 49 list* tmp=NULL; 50 tmp=Buy_node(); 51 tmp->_val=val; 52 tmp->_next=cur; 53 head->_next=tmp; 54 } 55 } 56 int Pop_node(list* head) 57 { 58 list* cur=head->_next; 59 if(cur) 60 { 61 list* tmp=cur->_next; 62 head->_next=tmp; 63 int ret=cur->_val; 64 free(cur); 65 cur->_next=NULL; 66 return ret; 67 } 68 else 69 { 70 return -1; 71 } 72 73 } 74 void destroy_list(list* head) 75 { 76 list* cur=head; 77 while(cur) 78 { 79 head=head->_next; 80 free(cur); 81 cur=NULL; 82 cur=head; 83 } 84 85 } 86 void Show_list(list* _head) 87 { 88 list* cur=_head->_next; 89 while(cur) 90 { 91 printf("%d\n",cur->_val); 92 cur=cur->_next; 93 } 94 } 95 void* product(void* arg) 96 { 97 while(1) 98 { 99 sleep(1);100 pthread_mutex_lock(&mutex);101 _datatype val=rand()%100;102 Push_node(head,val);103 104 printf("product:%d\n",val);105 pthread_mutex_unlock(&mutex);106 pthread_cond_signal(&cond);107 }108 return NULL;109 }110 void* consumer(void* arg)111 {112 113 while(1)114 {115 pthread_mutex_lock(&mutex);116 while(NULL==head->_next)117 {118 pthread_cond_wait(&cond,&mutex);119 }120 //pthread_mutex_lock(&mutex);121 122 int val= Pop_node(head);123 sleep(1);124 printf("consumer:%d\n",val);125 pthread_mutex_unlock(&mutex);126 }127 return NULL;128 }129 130 int main()131 {132 Init_list(&head);133 134 pthread_t t_product,t_consumer;135 pthread_create(&t_product,NULL,product,NULL);136 pthread_create(&t_consumer,NULL,consumer,NULL);137 138 pthread_join(t_product,NULL);139 pthread_join(t_consumer,NULL);140 141 pthread_mutex_destroy(&mutex);142 pthread_cond_destroy(&cond);143 144 145 return 0;146 }结果:gcc -o condition condition.c -lpthread[admin@localhost CONDITION]$ ./conditionproduct:83consumer:83product:86consumer:86product:77consumer:77product:15consumer:15product:93consumer:93product:35consumer:35product:86consumer:86...先进先出:... 37 void Push_node(list* head,_datatype val) 38 { 39 list* cur=head->_next; 40 if(cur==NULL) 41 { 42 list* tmp=NULL; 43 tmp=Buy_node(); 44 tmp->_val=val; 45 tmp->_next=NULL; 46 head->_next=tmp; 47 48 } 49 else 50 { 51 while(cur->_next!=NULL) 52 { 53 cur=cur->_next; 54 } 55 list* tmp=NULL; 56 tmp=Buy_node(); 57 tmp->_val=val; 58 tmp->_next=NULL; 59 cur->_next=tmp; 60 } 61 } ...101 void* product(void* arg)102 {103 while(1)104 {105 sleep(1);106 pthread_mutex_lock(&mutex);107 _datatype val=rand()%100;108 Push_node(head,val);109 printf("product:%d\n",val);110 pthread_mutex_unlock(&mutex);111 pthread_cond_signal(&cond);112 }113 return NULL;114 }115 void* consumer(void* arg)116 {117 118 while(1)119 {120 sleep(3);121 pthread_mutex_lock(&mutex);122 while(NULL==head->_next)123 {124 pthread_cond_wait(&cond,&mutex);125 }126 //pthread_mutex_lock(&mutex);127 128 int val= Pop_node(head);129 printf("consumer:%d\n",val);130 pthread_mutex_unlock(&mutex);131 }132 return NULL;133 }结果:[admin@localhost CONDITION]$ ./conditionproduct:83product:86consumer:83product:77product:15product:93consumer:86product:35
多消费者多生产者:
... 13 pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; 14 pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER; 15 pthread_mutex_t mutex3 = PTHREAD_MUTEX_INITIALIZER; ...104 void* product(void* arg)105 {106 while(1)107 {108 109 pthread_mutex_lock(&mutex2);110 pthread_mutex_lock(&mutex1);111 _datatype val=rand()%100;112 Push_node(head,val);113 printf("producer:tid:%ld,val:%d\n",pthread_self(),val);114 pthread_mutex_unlock(&mutex1);115 pthread_cond_signal(&cond);116 pthread_mutex_unlock(&mutex2);117 sleep(2);118 }119 return NULL;120 }121 void* consumer(void* arg)122 {123 while(1)124 {125 pthread_mutex_lock(&mutex3);126 pthread_mutex_lock(&mutex1);127 while(NULL==head->_next)128 {129 pthread_cond_wait(&cond,&mutex1);130 }131 //pthread_mutex_lock(&mutex);132 133 int val= Pop_node(head);134 printf("consumer:tid:%ld,val:%d\n",pthread_self(),val);135 pthread_mutex_unlock(&mutex1);136 pthread_mutex_unlock(&mutex3);137 sleep(1);138 }139 return NULL;140 }141 142 int main()143 {144 Init_list(&head);145 146 pthread_t tid;147 pthread_t producer1,producer2;148 pthread_t consumer1,consumer2;149 pthread_create(&producer1,NULL,product,NULL);150 151 pthread_create(&producer2,NULL,product,NULL);152 pthread_create(&consumer2,NULL,consumer,NULL);153 pthread_create(&consumer1,NULL,consumer,NULL);154 155 156 pthread_join(consumer1,NULL);157 pthread_join(consumer2,NULL);158 pthread_join(producer1,NULL);159 pthread_join(producer2,NULL);160 161 pthread_mutex_destroy(&mutex1);162 pthread_mutex_destroy(&mutex2);163 pthread_mutex_destroy(&mutex3);164 pthread_cond_destroy(&cond);165 166 167 return 0;168 }169结果:[admin@localhost CONDITION]$ ./conditionproducer:tid:-1227850896,val:83consumer:tid:-1248830608,val:83producer:tid:-1217361040,val:86consumer:tid:-1238340752,val:86producer:tid:-1227850896,val:77consumer:tid:-1248830608,val:77producer:tid:-1217361040,val:15consumer:tid:-1238340752,val:15^C 生产者与生产者之间互斥,消费者与消费者之间互斥,各自加个锁。
本文出自 “liveyoung” 博客,转载请与作者联系!
- 生产者-消费者模型
- 生产者/消费者模型
- 生产者 消费者模型
- 生产者消费者模型
- 生产者&&消费者模型
- 模拟生产者/消费者模型
- 生产者消费者模型
- 生产者消费者模型
- 生产者与消费者模型
- 生产者/消费者模型
- 生产者与消费者模型
- 生产者/消费者模型
- 生产者消费者模型
- 生产者消费者模型
- 生产者消费者模型
- 生产者消费者模型 ArrayBlockingQueue
- 生产者消费者模型
- 生产者消费者模型 - Storage
- RxJava操作符(02-创建操作)
- 分离线程
- Angular2入门——(4)为模板应用样式
- 线程同步
- 死锁问题
- 生产者消费者模型
- 一组数中,有两个数字各出现了一次,其他数字都是成对出现的,请找出这两个数。
- 读写锁
- 信号量 (线程互斥)
- 输入某二叉树的前序遍历和中序遍历的结果,请重建该二叉树。
- 两个栈实现一个队。
- 【JZOJ 3432】【OnlineJudge 1061】小M的服务器(含斜率优化解释)
- poj之旅——3484
- 请实现一个函数,把字符串中的每个空格替换成“%20”。