构建生产者与消费者的单链表模式

1、在linux环境下实现一个单链表
代码:

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<unistd.h>
typedef struct _node{
              int data;
          struct _node *next;
}node_t, *node_p, **node_pp;
static node_p alloc_node(int _d)
{
    node_p _n = (node_p)malloc(sizeof(node_t));
    if (NULL == _n){
      perror("malloc");
      exit(1);
    }
    _n->data = _d;
    _n->next = NULL;
}
static int is_empty(node_p _h)
{
    assert(_h);
    return _h->next == NULL? 1:0;
}
void init_list(node_pp _h)
{
    *_h = alloc_node(0);
}
void delete_node(node_p _tmp)
{
    if (_tmp){
           free(_tmp);
    }
}
void destroy_list(node_p _h)
{
   assert(_h);
   int data;
   while ( !is_empty(_h)){
          pop_list(_h, &data);
   }
   delete_node(_h);
}
void push_list(node_p _h, int _d)
{
    assert(_h);
    node_p _n = alloc_node(_d);
    _n->next = _h->next;
    _h->next = _n;
}
void pop_list(node_p _h, int *_o)
{
    assert(_h);
    assert(_o);
   if (!is_empty(_h)){
             node_p _tmp = _h->next;
        _h->next = _tmp->next;
        *_o = _tmp->data;
        delete_node(_tmp);
   }
}
void show_list(node_p _h)
{
    assert(_h);
    node_p p =  _h->next;
    while (p != NULL){
           printf(" %d ", p->data);
           p = p->next;
    }
    printf("\n");
}
int main()
{
    node_p  head = NULL;
    init_list(&head);
    int i = 0;
        for (; i<10; i++){
               push_list(head, i);
               show_list(head);
               sleep(1);
    }
        int data = 0;
        for (; i>5; i--){
               pop_list(head, &data);
                       show_list(head);
               sleep(1);
        }
    destroy_list(head);
    return 0;
}

运行结果如图所示:
这里写图片描述
条件变量:函数pthread_cond_init()
pthread_cond_wait()—获得某种资源的执行流的等待的方式
pthread_cond_signal();—唤醒在该条件下等待的执行流
条件变量总是和Mutex搭配使⽤。
⼀个线程可以调⽤用pthread_cond_wait在一个Condition Variable上阻塞等待,这个函数做以下三步操作:
1. 释放Mutex
2. 阻塞等待
3. 当被唤醒时,重新获得Mutex并返回
构建一个生产者与一个消费者模式,创建两个线程在主函数中,代码如下:

    pthread_t id1, id2;
    pthread_create(&id1, NULL, consumer, head);
    pthread_create(&id2, NULL, producter, head);
    pthread_join(id1, NULL);
    pthread_join(id2, NULL);

生产者-消费者,生产者生产⼀一个结构体串在链表的表头上,消费者 从表头取⾛走结构体,代码如下:

static pthread_mutex_t  lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t  needproduct = PTHREAD_COND_INITIALIZER;
static void *consumer(void *arg)
{
    node_p h = (node_p)arg;
    for ( ; ; ){
           pthread_mutex_lock(&lock);
           int data = rand()%1234;
           while (! is_empty(h)){

               printf("consumer done...,consumer wait...%d\n", data);
                pthread_cond_wait(&needproduct,&lock);
                printf("consumer wakeup...\n");

       }
     pop_list(h, &data);
     pthread_mutex_unlock(&lock);
    }
}
static void *producter(void *arg)
{
    node_p h = (node_p)arg;
    for( ; ; ){
        pthread_mutex_lock(&lock);
        int data = rand()%1234;
        push_list(h, &data);
        printf("producter done...: %d\n", data);
        sleep(1);
        pthread_mutex_unlock(&lock);
        pthread_cond_signal(&needproduct);
        printf("data is ok, signal consumer!\n");
    }
}  

运行结果:
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值