Chinaunix首页 | 论坛 | 博客
  • 博客访问: 563121
  • 博文数量: 142
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1452
  • 用 户 组: 普通用户
  • 注册时间: 2013-09-12 16:28
文章分类

全部博文(142)

文章存档

2016年(10)

2015年(60)

2014年(72)

我的朋友

分类: C/C++

2014-12-15 11:16:36


点击(此处)折叠或打开

  1. #include "unipc.h"

  2. #define MAXNITIEMS 1000000
  3. #define MAXNTHREADS 100

  4. int nitems;

  5. /*critical region*/
  6. int buff[MAXNITIEMS]; /*shared data array*/
  7. struct {
  8.     pthread_mutex_t mutex; /*mutex*/
  9.     int nput;
  10.     int nval;
  11. } put = {
  12.     PTHREAD_MUTEX_INITIALIZER,0,0
  13. };

  14. struct {
  15.     pthread_mutex_t mutex;
  16.     pthread_cond_t cond;
  17.     int nready;
  18. } cond;

  19. int
  20. set_concurrency(int level)
  21. {
  22. #ifdef HAVE_THR_SETCONCURRENCY_PROTO
  23.     int thr_setconcurrency(int);

  24.     return(thr_setconcurrency(level));
  25. #else
  26.     return(0);
  27. #endif
  28. }

  29. void
  30. Set_concurrency(int level)
  31. {
  32.     if (set_concurrency(level) != 0){
  33.         printf("set_concurrency error\n");
  34.     }
  35. }

  36. void *produce(void *),*consume(void*);



  37. void *produce(void *arg)
  38. {
  39.     for(;;) {
  40.         pthread_mutex_lock(&put.mutex);
  41.         if(put.nput >= nitems) {
  42.             pthread_mutex_unlock(&put.mutex);
  43.             return NULL;
  44.         }
  45.         buff[put.nput++] = put.nval++;
  46.         pthread_mutex_unlock(&put.mutex);

  47.         pthread_mutex_lock(&cond.mutex);
  48.         if(cond.nready == 0)
  49.             pthread_cond_signal(&cond.cond);
  50.         cond.nready++;
  51.         pthread_mutex_unlock(&cond.mutex);

  52.         (*(int*)arg)++;
  53.     }
  54.     return NULL;
  55. }

  56. void consume_wait()
  57. {
  58.     pthread_mutex_lock(&cond.mutex);
  59.     while(cond.nready == 0)
  60.         pthread_cond_wait(&cond.cond,&cond.mutex);
  61.     cond.nready--;
  62.     pthread_mutex_unlock(&cond.mutex);
  63. }

  64. void *consume(void *arg)
  65. {
  66.     int i;
  67.     for(i = 0;i < nitems;i++) {
  68.         consume_wait();
  69.         if(i != buff[i]) {
  70.             printf("i = %d,buffer[i] = %d\n",i,buff[i]);
  71.         }
  72.     }
  73.     return NULL;
  74. }
  75. int min(int a, int b)
  76. {
  77.     if(a < b) {
  78.         return a;
  79.     }else {
  80.         return b;
  81.     }
  82. }
  83. int main(int argc, char *argv[])
  84. {
  85.     int i, nthreads,count[MAXNTHREADS];
  86.     pthread_t tid_produce[MAXNTHREADS],tid_consume;


  87.     if(argc != 3) {
  88.         printf("usage: produces2 <#item> <#threads>\n");
  89.         return -1;
  90.     }

  91.     nitems = min(atoi(argv[1]),MAXNITIEMS);
  92.     nthreads = min(atoi(argv[2]),MAXNTHREADS);

  93.     //Set_concurrency(nthreads+1);

  94.     for(i = 0; i < nthreads; i++)
  95.     {
  96.         count[i] = 0;
  97.         pthread_create(&tid_produce[i],NULL,produce,&count[i]);
  98.     }

  99.     pthread_create(&tid_consume,NULL,consume,NULL);
  100.     
  101.     for(i = 0; i < nthreads; i++) {
  102.         pthread_join(tid_produce[i],NULL);
  103.         printf("thread %zu, count=%d\n",tid_produce[i],count[i]);
  104.     }


  105.     pthread_join(tid_consume,NULL);

  106.     exit(0);
  107. }

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