700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Linux系统编程:使用mutex互斥锁和条件变量实现多个生成者和消费者模型

Linux系统编程:使用mutex互斥锁和条件变量实现多个生成者和消费者模型

时间:2022-12-14 05:21:44

相关推荐

Linux系统编程:使用mutex互斥锁和条件变量实现多个生成者和消费者模型

实现代码

如题,使用mutex互斥锁和条件变量实现多个生成者和消费者模型

直接上代码,需要线程中的互斥锁和条件变量的相关知识进行支撑。这里就不细说了呀,代码中有一定的注释。

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <fcntl.h>#include <error.h>#include <sys/errno.h>#include <pthread.h>//产品typedef struct Msg{struct Msg* next;int num;}Msg;pthread_mutex_t lock =PTHREAD_MUTEX_INITIALIZER; //静态初始化mutex 互斥锁,也可以使用pthread_mutex_init方法进出初始化pthread_cond_t cond = PTHREAD_COND_INITIALIZER; //初始化条件变量Msg* head = NULL;//生产者void* produce(void* arg){Msg* msg = NULL;int i = (int)arg;//序号while(1){msg = (Msg*)malloc(sizeof(Msg));msg->num = rand()%1000;//添加到公共区域pthread_mutex_lock(&lock);msg->next = head;head = msg;pthread_mutex_unlock(&lock);printf("%dth ***producer***:%d\n",i,msg->num);pthread_cond_broadcast(&cond);sleep(rand()%3);}return NULL;}void* custome(void* arg){Msg* msg = NULL;int i = (int)arg;//序号while(1){pthread_mutex_lock(&lock);//为什么 while而不是if ?当没有产品时,多个消费者线程有可能同时阻塞在cond_wait。//当产品区有一个产品,被唤醒的一个消费者将产品消费,其他产品再次消费时,需要判断产品区是否有产品while( head == NULL ){pthread_cond_wait(&cond,&lock);}//消费msg = head;head = msg->next;pthread_mutex_unlock(&lock);printf("%dth ###customer###:%d\n",i,msg->num);free(msg);sleep(rand()%3);}return NULL;}int main(int argc,char* argv[]){srand((unsigned int)time(NULL));pthread_t pro[3],cus[5];int i;//3 个生产者线程for(i = 0 ; i <3; i++){pthread_create(&pro[i],NULL,produce,(void*)i);}//5 个消费者线程for( i = 0; i < 5; i++ ){pthread_create(&cus[i],NULL,custome,(void*)i);}pthread_mutex_destroy(&lock);//主线程回收线程for( i = 0; i < 3; i++ ){pthread_join(pro[i],NULL);}for( i = 0; i < 5; i++ ){pthread_join(cus[i],NULL);}return 0;}

实现效果

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。