生产者与消费者模式总结

理论:https://blog.csdn.net/liushall/article/details/81569609

代码:

#include  <stdio.h>
#include  <pthread.h>
#include  <unistd.h>
#include  <stdlib.h>
#include  <assert.h>

typedef struct Node{
    int data;
    struct Node *next;
}Node,*Node_p,**Node_pp;
 Node_p list=NULL;
 pthread_mutex_t mylock=PTHREAD_MUTEX_INITIALIZER;
 pthread_cond_t mycond=PTHREAD_COND_INITIALIZER;
 /**
  * @Func:Node_p allocNode(int data)
  * @Brief:分配节点,采用的是尾插发
  * @Para :data
  * @return:Node_p
  * 
  */
 Node_p allocNode(int data)
 {
     Node_p n=(Node_p)malloc(sizeof(Node));
     if(n==NULL)
     {
        return NULL;
     }
     n->data=data;
     n->next=NULL;
     return n;

 }
 /**
  * 
  * 初始化链表
  * 
  */
void Init(Node_pp list)
{
   *list=allocNode(0);
   Node_pp _n=(Node_pp)malloc(sizeof(list));

   if(_n==NULL){
      return ;
   }       
}
  /**
   *@brief:判断链表是否为空 
   */
int isEmpty(Node_p list)
{
   assert(list);
   if(list->next==NULL){
      return 1;
   }else
   {
      return 0;
   }
   
}
  /**
   * @func:showlist
   * @brief:(打印链表)
   * @para:list
   * @return:NULL
   * @date:
   * @log:
   */
void Showlist(Node_p list)
{
   assert(list);
   Node_p  cur=list->next;
   while(cur->next)
   {
      printf("%d->",cur->data);
      cur=cur->next;
   }
   printf("\n");
}
  /*
   @brief:头部压入数据
  */
void  PushFront(Node_p list,int data)
{
   assert(list);
   Node_p n=allocNode(data);
   if(n==NULL){
      perror("push");
      
   }else
   {
         n->next=list->next;
      list->next=n;
   }
   
}

  /*c从头部弹出链表数据*/
void  PopFront(Node_p list,int  *data)
{
   if(!isEmpty(list)){
      Node_p del=list->next;
      list->next=del->next;
      *data=del->data;
      assert(list);
      free(del);
   }else
   {
      printf("link list is  empty");
   }
   
}
  /*
  @brief:销毁链表
  */
void destory(Node_p list)
{
   int data;
   assert(list);
   while(!isEmpty(list))
   {
      PopFront(list,&data);
   }
   free(list);
}
  /*
   * 生产者
  */
 void * producer(void *arg)
 {
    int data;
    while(1){
       usleep(100000);
       data=rand()%100+1;
       pthread_mutex_lock(&mylock);
       PushFront(list,data);
       pthread_mutex_unlock(&mylock);
       pthread_cond_signal(&mycond);
       printf("Producer:%d\n",data);
       
    }
    return NULL;
 }
  /**
   * 消费 
   * 
   */
void * Consumer(void *arg)
{
   int data=0;
   while(1)
   {
      pthread_mutex_lock(&mylock);
      while(isEmpty(list)){
         //阻塞等待
         pthread_cond_wait(&mycond,&mylock);
      }
      PopFront(list,&data);
      pthread_mutex_unlock(&mylock);
      printf("consumer:%d\n",data);
      
   }
   return NULL;
}
int main()
{
   Init(&list);
   pthread_t tid1,tid2; //创建
   pthread_create(&tid1,NULL,Consumer,NULL);
   pthread_create(&tid2,NULL,producer,NULL);
   pthread_join(tid1,NULL);//阻塞线程退出
   pthread_join(tid2,NULL);
   destory(list);
   pthread_mutex_destroy(&mylock);
   pthread_cond_destroy(&mycond);//销毁条件变量

}
发布了102 篇原创文章 · 获赞 26 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_42145502/article/details/104030024