博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
链表实现生产者消费者
阅读量:4097 次
发布时间:2019-05-25

本文共 1838 字,大约阅读时间需要 6 分钟。

 

#include<stdio.h>                                 

#include<stdlib.h>
#include<pthread.h>

pthread_mutex_t lock;
pthread_cond_t cond;

typedef struct _Node{

    struct _Node* next;
    int data;
}node_t,*node_p,**node_pp;

node_p alloc_node(int x){ 
    node_p p=(node_p)malloc(sizeof(node_t));
    if(!p){
        perror("malloc");
        exit(1);
    }   

   p->data=x;

   p->next=NULL;
   return p;
}
void init_list(node_pp h){ 
    if(h==NULL){
        return;
    }   
    *h=alloc_node(0);
}

void list_push(node_p h,int x){ 
    node_p p=alloc_node(x);
    p->next=h->next;
    h->next=p;
}

int is_empty(node_p head){

    return head->next==NULL?1:0;
}
void list_pop(node_p head,int* x){ 
    if(!is_empty(head)){
        node_p p=head->next;
        *x=p->data;
        head->next=p->next;
        free(p);
    }   
}
void list_destroy(node_p h){ 
    int x;
    while(!is_empty(h)){
       list_pop(h,&x) ;
    }   
    free(h);
}

void show_list(node_p h){ 

    if(!is_empty(h)){
        node_p p=h->next;
        while(p){
            printf("%d ",p->data);
            p=p->next;
        }
        printf("\n");
    }
}
void* consumer(void* arg){
    node_p head=(node_p)arg;
    while(1){
       pthread_mutex_lock(&lock);
       if(!is_empty(head)){
           int data;
           list_pop(head,&data);
           printf("consumer done! data is:%d\n",data);
       }else{
           printf("consumer waiting!\n");
           pthread_cond_wait(&cond,&lock);
       }
       pthread_mutex_unlock(&lock);
       sleep(3);
    }
}
void* product(void* arg){
    while(1){
        node_p head=(node_p)arg;
        if(is_empty(head)){//通过判断来避免消费者慢生产者快的问题
            int data;
            data=rand()%100+1;
            pthread_mutex_lock(&lock);
            list_push(head,data);
            pthread_cond_signal(&cond);
            printf("product done! data is:%d\n",data);
            pthread_mutex_unlock(&lock);
        }else{
            printf("product waiting!\n");
        }
        sleep(1);
    }
}
int main(){

   node_p head;

   init_list(&head);
   pthread_mutex_init(&lock,NULL);
   pthread_cond_init(&cond,NULL);

   pthread_t c,p;

   pthread_create(&c,NULL,consumer,(void*)head);
   pthread_create(&p,NULL,product,(void*)head);

   pthread_join(c,NULL);                                                                                                                                                                                                                                    

   pthread_join(p,NULL);
   list_destroy(head);
   pthread_mutex_destroy(&lock);
   pthread_cond_destroy(&cond);
}

转载地址:http://pwlii.baihongyu.com/

你可能感兴趣的文章
Koa2初体验
查看>>
Koa 2 初体验(二)
查看>>
Koa2框架原理解析和实现
查看>>
vue源码系列文章good
查看>>
你不知道的Virtual DOM
查看>>
VUE面试题总结
查看>>
写好JavaScript条件语句的5条守则
查看>>
原生JS中DOM节点相关API合集
查看>>
【TINY4412】U-BOOT移植笔记:(7)SDRAM驱动
查看>>
C++虚函数的总结
查看>>
什么是URL地址?
查看>>
C++多态的实现方式总结
查看>>
学习C++需要注意的问题
查看>>
C++模板
查看>>
C++双冒号(::)的用法
查看>>
【Unity】封装SQLite管理类
查看>>
【Unity】面试题整理
查看>>
【C#】如何实现一个迭代器
查看>>
【Unity】Destroy和DestroyImmediate的区别
查看>>
【Lua】Mac系统下配置SublimeText的Lua编译环境
查看>>