基于链表队列的单个个生产者与消费者模型:
#include<stdio.h> #include<stdlib.h> #include<pthread.h> static pthread_mutex_t lock=PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t cond=PTHREAD_COND_INITIALIZER; typedef struct node { int data; struct node* next; }Node; Node* initList() { Node* head=(Node*)malloc(sizeof(Node)); head->data=0; return head; } Node* head=NULL; //Node* head=initList();//这种形式是错误的,可以在函数内部这样调用;不可以将函数的返回值赋给全局变量; Node* allocNode(int value) { Node* creatNode=(Node*)malloc(sizeof(Node)); creatNode->next=NULL; creatNode->data=value; return creatNode; } void pushNode(Node* head,int value) { Node* newNode=allocNode(value); if(head->next==NULL) { head->next=newNode; } else { Node* nextNode=head->next; newNode->next=nextNode; head->next=newNode; } //printf("%d",value); } int popNode(Node* head) { Node* cur=NULL; if(head->next) { cur=head->next; head->next=cur->next; } int value=cur->data; free(cur); //printf("%d",value); return value; } int isEmpty(Node* head) { return ((head->next==NULL)?0:-1); } void* consume(void* arg) { int count=0; while(count++<100) { pthread_mutex_lock(&lock);// if(isEmpty(head)==0) { printf("no data,consume wait \n"); pthread_cond_wait(&cond,&lock);// // } if(isEmpty(head)==-1) { printf("consumer:%d\n",popNode(head)); } pthread_mutex_unlock(&lock);// } } void* product(void* arg) { int i=0; for(i;i<10;i++) { pthread_mutex_lock(&lock);// pushNode(head,i); printf("product doing:%d\n",i); pthread_mutex_unlock(&lock);// pthread_cond_signal(&cond); sleep(1); } } void print(Node* head) { Node* cur=head->next; while(cur) { printf("%d",cur->data); cur=cur->next; } printf("\n"); } int main() { head=initList(); pthread_t product_t; pthread_t consume_t; pthread_create(&product_t,NULL,product,NULL); pthread_create(&consume_t,NULL,consume,NULL); pthread_join(product_t,NULL); pthread_join(consume_t,NULL); }
基于环形队列的多个生产者与消费者模型:
sem_t semBlank; sem_t semData; int ringBuf[10]; void initCircle() { ringBuf[10]=-1; sem_init(&semBlank,0,10); sem_init(&semData,0,0); } void* product(void* arg) { int step=0; while(1) { sem_wait(&semBlank); int data=rand()%64; ringBuf[step]=data; printf("product:%d\n",data); sem_post(&semData); step++; sleep(1); step%=10; } } void* consum(void* arg) { int step=0;//不能将step放到while循环里面; while(1) { sem_wait(&semData); printf("consum:%d\n",ringBuf[step]); ringBuf[step]=-1; sem_post(&semBlank); step++; step%=10; } } int main() { initCircle(); pthread_t product_t; pthread_t consum_t; pthread_create(&product_t,NULL,product,NULL); pthread_create(&consum_t,NULL,consum,NULL); pthread_join(product_t,NULL); pthread_join(consum_t,NULL); }
基于环形队列的多个生产者与消费者模型://注意需要定义两把锁,生产者之间一把,消费者之间一把,生产者与消费着之间不需要,因为有p,v操作;
#include<stdio.h> #include<stdlib.h> #include<pthread.h> #include<semaphore.h> static pthread_mutex_t lock_p=PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t lock_c=PTHREAD_MUTEX_INITIALIZER; sem_t semBlank; sem_t semData; int step_c=0; int step_p=0; int ringBuf[10]; void initCircle() { ringBuf[10]=-1; sem_init(&semBlank,0,10); sem_init(&semData,0,0); } void* product(void* arg) { while(1) { pthread_mutex_lock(&lock_p); sem_wait(&semBlank); int data=rand()%64; ringBuf[step_p]=data; printf("product:%d\n",data); sem_post(&semData); step_p++; sleep(1); step_p%=10; pthread_mutex_unlock(&lock_p); } } void* consum(void* arg) { while(1) { pthread_mutex_lock(&lock_c); sem_wait(&semData); printf("consum:%d\n",ringBuf[step_c]); ringBuf[step_c]=-1; sem_post(&semBlank); step_c++; step_c%=10; pthread_mutex_unlock(&lock_c); } } int main() { initCircle(); pthread_t product_t1; pthread_t product_t2; pthread_t consum_t1; pthread_t consum_t2; pthread_create(&product_t1,NULL,product,NULL); //注意理解多生产者并不是创建多个函数,而是给临界资源(全局变量step)加锁; pthread_create(&product_t2,NULL,product,NULL); pthread_create(&consum_t1,NULL,consum,NULL); pthread_create(&consum_t2,NULL,consum,NULL); pthread_join(product_t1,NULL); pthread_join(product_t2,NULL); pthread_join(consum_t1,NULL); pthread_join(consum_t2,NULL); }
生产者与消费者
最新推荐文章于 2023-05-07 12:29:28 发布