Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6720
  • 博文数量: 5
  • 博客积分: 28
  • 博客等级: 民兵
  • 技术积分: 35
  • 用 户 组: 普通用户
  • 注册时间: 2012-08-10 16:53
文章分类
文章存档

2012年(5)

我的朋友
最近访客

分类:

2012-10-25 19:21:31

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

点击(此处)折叠或打开

  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.  }

阅读(610) | 评论(0) | 转发(0) |
0

上一篇:lvs 三种模式详细介绍

下一篇:没有了

给主人留下些什么吧!~~