Linux系统编程 | 使用条件变量实现生产者和消费者模型

代码示例
   #include <stdio.h>
   #include <stdlib.h>
   #include <string.h>
   #include <unistd.h>
   #include <sys/types.h>
   #include <pthread.h>
   
   typedef struct node
  {
          int data;
          struct node *next;
  }NODE;
 
  NODE *head = NULL;
  
  //定义一把锁
  pthread_mutex_t mutex;
  //定义一个条件变量
  pthread_cond_t cond;
  
  
  //生产者线程
  void *producer(void *arg)
  {
          NODE *pNode = NULL;
          while(1)
          {
                  //生产一个节点
                  pNode = (NODE*)malloc(sizeof(NODE));
                  if(pNode == NULL)
                  {
                          perror("malloc error");
                          exit(-1);
                  }
  
                  pNode->data = rand() % 1000;
                  printf("p:[%d]\n",pNode->data);
  
                  //加锁
                  pthread_mutex_lock(&mutex);
                  pNode->next = head;
                  head = pNode;
  
                  //解锁
                  pthread_mutex_unlock(&mutex);
  
                  //通知消费者线程解除阻塞
                  pthread_cond_signal(&cond);
                  sleep(rand()%3);
  
  
         }
  }
  
  //消费者线程
  void *consumer(void *arg)
  {
          NODE *pNode = NULL;
          while(1)
          {
  
                  //加锁
                  pthread_mutex_lock(&mutex);
  
                  if(head == NULL)
                  {
                          //若条件不满足,需要阻塞等待
                          //若条件不满足,则阻塞等待并解锁
                          //若条件满足(被生产者线程调用pthread_cond_signal函>    数通知),解除阻塞并加锁
                          pthread_cond_wait(&cond,&mutex);
                  }
                  printf("C:[%d]\n",head->data);
                  pNode = head;
                  head = head->next;
  
                  //解锁
                  pthread_mutex_unlock(&mutex);
                  free(pNode);
                  pNode = NULL;
  
                  sleep(rand()%3);
  
          }
  }
  
  
  int main()
  {
          int ret;
          pthread_t thread1;
          pthread_t thread2;
  
          //初始化互斥锁
          pthread_mutex_init(&mutex,NULL);
  
          //条件变量初始化
          pthread_cond_init(&cond,NULL);
  
          //创建生产者线程
         ret = pthread_create(&thread1,NULL,producer,NULL);
         if(ret != 0)
         {
                 printf("pthread_creat error,[%s]\n",strerror(ret));
                 return -1;
         }
 
         ret = pthread_create(&thread2,NULL,consumer,NULL);
         if(ret != 0)
         {
                 printf("pthread_creat error,[%s]\n",strerror(ret));
                 return -1;
         }
 
         //等待线程结束
         pthread_join(thread1,NULL);
         pthread_join(thread2,NULL);
 
         //释放互斥锁
         pthread_mutex_destroy(&mutex);
 
         //释放条件变量
         pthread_cond_destroy(&cond);
 
         return 0;
 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值