Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2621387
  • 博文数量: 315
  • 博客积分: 3901
  • 博客等级: 少校
  • 技术积分: 3640
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-08 15:32
个人简介

知乎:https://www.zhihu.com/people/monkey.d.luffy Android高级开发交流群2: 752871516

文章分类

全部博文(315)

文章存档

2019年(2)

2018年(1)

2016年(7)

2015年(32)

2014年(39)

2013年(109)

2012年(81)

2011年(44)

分类: C/C++

2012-10-24 20:53:12

经测试貌似没有问题!下一步,“线程池”....

点击(此处)折叠或打开

  1. /*
  2.   * produce_consumer.c
  3.   *
  4.   * Created on: Oct 24, 2012
  5.   * Author: hl
  6.   */
  7.  #include <stdio.h>
  8.  #include <stdlib.h>
  9.  #include <pthread.h>
  10.  #include <string.h>
  11.  #include <unistd.h>
  12.  
  13.  //消息头
  14.  struct msg
  15.  {
  16.      int num;
  17.      struct msg * next;
  18.  };
  19.  struct msg * head;
  20.  
  21.  pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;    /// < 互斥锁
  22.  pthread_cond_t cond = PTHREAD_COND_INITIALIZER;        /// < 唤醒机制
  23.  
  24.  /**
  25.   * @brief 消费者
  26.   * @param 修改:
  27.   *         - 打算从尾部消费(否则较早生产的总是消费不到,过期了!)
  28.   */
  29.  void * consumer(void * pVoid)
  30.  {
  31.      struct msg * mp;
  32.      struct msg * mpp;
  33.      while (1)
  34.      {
  35.          pthread_mutex_lock(&mutex);
  36.          if (NULL == head)
  37.              pthread_cond_wait(&cond, &mutex);
  38.          mp = head;
  39.          if (mp->next) /// < 超过一个结点
  40.          {
  41.              while (mp->next)
  42.              {
  43.                  mpp = mp; /// < 前一个
  44.                  mp = mp->next; /// < 链表最后一个
  45.              }
  46.           printf("Consume %d\n", mp->num);
  47.           free(mp);
  48.           mpp->next = mp = NULL; /// < 必须的,且注意mpp->next!
  49.          }
  50.          else /// < 只有头结点的情况
  51.          {
  52.           printf("Consume %d\n", mp->num);
  53.           free(mp);
  54.           head = mp = NULL; /// < 必须的,且注意head!
  55.          }
  56.          pthread_mutex_unlock(&mutex);
  57.          sleep(2);
  58.      }
  59.      return pVoid;
  60.  }
  61.  
  62.  /**
  63.   * @brief 生产者
  64.   */
  65.  void * producer(void * pVoid)
  66.  {
  67.      struct msg * mp;
  68.      while (1)
  69.      {
  70.          mp = (struct msg *)malloc(sizeof(struct msg));
  71.          mp->num = rand()%1000 + 1;
  72.          printf("Produce %d\n", mp->num);
  73.          pthread_mutex_lock(&mutex);
  74.          mp->next = head;
  75.          head = mp;
  76.          pthread_mutex_unlock(&mutex);
  77.          pthread_cond_signal(&cond);
  78.          sleep(1);
  79.      }
  80.      return pVoid;
  81.  }
  82.  
  83.  
  84.  
  85.  int main(void)
  86.  {
  87.      pthread_t pid;
  88.      pthread_t cid;
  89.  
  90.      head = NULL;
  91.  
  92.      int err;
  93.      err = pthread_create(&pid, NULL, producer, "producer");
  94.      if (0 != err)
  95.      {
  96.          fprintf(stderr, "create thread1 failed:----!%s\n", strerror(err));
  97.          return -1;
  98.      }
  99.  
  100.      err = pthread_create(&cid, NULL, consumer, "consumer");
  101.      if (0 != err)
  102.      {
  103.          fprintf(stderr, "create thread2 failed:----!%s\n", strerror(err));
  104.          return -1;
  105.      }
  106.      pthread_join(pid, NULL);
  107.      pthread_join(cid, NULL);
  108.  
  109.      return 0;
  110.  }

阅读(3970) | 评论(0) | 转发(3) |
给主人留下些什么吧!~~