Chinaunix首页 | 论坛 | 博客
  • 博客访问: 469038
  • 博文数量: 83
  • 博客积分: 92
  • 博客等级: 民兵
  • 技术积分: 690
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-25 13:33
文章分类

全部博文(83)

文章存档

2014年(3)

2013年(59)

2012年(16)

2011年(5)

我的朋友

分类: LINUX

2013-04-28 09:00:54

  1. form:luozhiyong131.blog.chinaunix.net


  2. /*
  3.  * Produce/Consume using multithread
  4.  */

  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <pthread.h>
  8. #include <string.h>
  9. #include <assert.h>
  10. #include <time.h>
  11. #include <unistd.h>
  12. #include <errno.h>

  13. #define BSIZE    30        //buffer size
  14. #define PRODUCE_EXIT    '~'    //produce thread exit flag
  15. #define CONSUME_EXIT    '@'    //consume thread exit flag
  16. #define PRODUCE_NUMBER    3    //produce thread number
  17. #define CONSUME_NUMBER    5    //consume thread number

  18. typedef struct buffer_t
  19. {
  20.     char            buf[BSIZE];
  21.     int                occupied;    //product number
  22.     int                nextin;
  23.     int                nextout;
  24.     pthread_mutex_t    mutex;
  25.     pthread_cond_t    less;
  26.     pthread_cond_t    more;
  27. }buffer_t;

  28. pthread_once_t once = PTHREAD_ONCE_INIT;

  29. buffer_t ware;

  30. /*
  31.  * initialization
  32.  */
  33. void init(void)
  34. {
  35.     memset(&ware, 0, sizeof(ware));
  36.     pthread_mutex_init(&ware.mutex, NULL);
  37.     pthread_cond_init(&ware.less, NULL);
  38.     pthread_cond_init(&ware.more, NULL);
  39. }
  40. /*
  41.  * destroy
  42.  */
  43. void destroy(void)
  44. {
  45.     pthread_mutex_destroy(&ware.mutex);
  46.     pthread_cond_destroy(&ware.less);
  47.     pthread_cond_destroy(&ware.more);
  48. }

  49. /*
  50.  * The produce thread
  51.  */
  52. void * threadProduce(void * arg)
  53. {
  54.     char c = 0;
  55.     struct timespec tt;
  56.     int result;
  57.     
  58.     while(c != PRODUCE_EXIT)
  59.     {
  60.         //read product from keyboard
  61.         c = getchar();
  62.         if(c == '\n')
  63.             continue;
  64.         
  65.         //lock mutex
  66.         assert( pthread_mutex_lock(&ware.mutex) == 0);
  67.         //condition wait
  68.         tt.tv_sec = time(NULL)+1;
  69.         tt.tv_nsec= 0;
  70.         if(ware.occupied >= BSIZE)
  71.         {
  72.             result = pthread_cond_timedwait(
  73.                         &ware.more,
  74.                         &ware.mutex,
  75.                         &tt);
  76.             if(result == ETIMEDOUT)
  77.             {
  78.                 assert( pthread_mutex_unlock(&ware.mutex) == 0);
  79.                 continue;
  80.             }else if(result != 0)
  81.             {
  82.                 printf("pthread_cond_timedwait ERROR.\n");
  83.                 assert( pthread_mutex_unlock(&ware.mutex) == 0);
  84.                 pthread_exit(NULL);
  85.             }
  86.         }
  87.         //insert product into buf
  88.         ware.buf[ware.nextin] = c;
  89.         ware.occupied++;
  90.         ware.nextin = (ware.nextin+1)%BSIZE;

  91.         printf("produce: [tid=%u] [%c].\n",
  92.                 pthread_self(), c);
  93.         
  94.         //aware the consume thread
  95.         pthread_cond_signal(&ware.less);
  96.         //unlock mutex
  97.         assert( pthread_mutex_unlock(&ware.mutex) == 0);
  98.         sleep(1);
  99.     }
  100.     printf("produce [tid=%u] EXIT.\n", pthread_self());
  101.     pthread_exit(NULL);
  102. }

  103. /*
  104.  * The consume thread
  105.  */
  106. void * threadConsume(void * arg)
  107. {
  108.     char c = 0;
  109.     struct timespec tt;
  110.     int result;
  111.     
  112.     while(c != CONSUME_EXIT)
  113.     {
  114.         //lock mutex
  115.         assert( pthread_mutex_lock(&ware.mutex) == 0);
  116.         //condition wait
  117.         tt.tv_sec = time(NULL)+1;
  118.         tt.tv_nsec= 0;
  119.         if(ware.occupied <= 0)
  120.         {
  121.             result = pthread_cond_timedwait(
  122.                         &ware.less,
  123.                         &ware.mutex,
  124.                         &tt);
  125.             if(result == ETIMEDOUT)
  126.             {
  127.                 assert( pthread_mutex_unlock(&ware.mutex) == 0);
  128.                 continue;
  129.             }else if(result != 0)
  130.             {
  131.                 printf("pthread_cond_timedwait ERROR.\n");
  132.                 assert( pthread_mutex_unlock(&ware.mutex) == 0);
  133.                 pthread_exit(NULL);
  134.             }
  135.         }
  136.         //read product from buf
  137.         c = ware.buf[ware.nextout];
  138.         ware.occupied--;
  139.         ware.nextout = (ware.nextout+1)%BSIZE;
  140.         printf("consume: [tid=%u] [%c].\n",
  141.                 pthread_self(), c);
  142.         
  143.         //aware the produce thread
  144.         pthread_cond_signal(&ware.more);
  145.         //unlock mutex
  146.         assert( pthread_mutex_unlock(&ware.mutex) == 0);
  147.         sleep(1);
  148.     }
  149.     printf("consume [tid=%u] EXIT.\n", pthread_self());
  150.     pthread_exit(NULL);
  151. }
  152.                         
  153. int main(void)
  154. {
  155.     pthread_t tid[PRODUCE_NUMBER+CONSUME_NUMBER];
  156.     int i;
  157.     
  158.     pthread_once(&once, init);
  159.     for(i=0; i<PRODUCE_NUMBER; i++)
  160.     {
  161.         if( pthread_create(&tid[i], NULL,
  162.                     threadProduce, NULL) )
  163.         {
  164.             printf("pthread_create ERROR.\n");
  165.             destroy();
  166.             exit(-1);
  167.         }
  168.     }
  169.     for(; i<PRODUCE_NUMBER+CONSUME_NUMBER; i++)
  170.     {
  171.         if( pthread_create(&tid[i], NULL,
  172.                     threadConsume, NULL) )
  173.         {
  174.             printf("pthread_create ERROR.\n");
  175.             destroy();
  176.             exit(-1);
  177.         }
  178.     }
  179.     
  180.     for(i=0; i<PRODUCE_NUMBER+CONSUME_NUMBER; i++)
  181.     {
  182.         pthread_join(tid[i], NULL);
  183.     }
  184.     destroy();
  185.     
  186.     pthread_exit(NULL);
  187. }
  1. /*
  2.  * 线程同步
  3.  */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <pthread.h>
  7. #include <assert.h>

  8. int product = 0;

  9. pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  10. pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;

  11. /*
  12.  * 线程函数 读
  13.  */
  14. void * threadRead(void * arg)
  15. {
  16.     int cnt = 0;
  17.     while(cnt++ < 100)
  18.     {
  19. //        assert( pthread_mutex_lock(&mutex) == 0);
  20.         assert( pthread_rwlock_rdlock(&rwlock) == 0);
  21.         
  22.         printf("Read: product = %d\n", product);
  23.         
  24. //        assert( pthread_mutex_unlock(&mutex) == 0);
  25.         assert( pthread_rwlock_unlock(&rwlock) == 0);
  26.         sleep(1);
  27.     }
  28.     pthread_exit(NULL);
  29. }

  30. /*
  31.  * 线程函数 写 增加
  32.  */
  33. void * threadProduce(void * arg)
  34. {
  35.     int cnt = 0;
  36.     while(cnt++ < 100)
  37.     {
  38. //        assert( pthread_mutex_lock(&mutex) == 0);
  39.         assert( pthread_rwlock_wrlock(&rwlock) == 0);
  40.         
  41.         product++;
  42.         printf("Produce: product = %d\n", product);
  43.         
  44. //        assert( pthread_mutex_unlock(&mutex) == 0);
  45.         assert( pthread_rwlock_unlock(&rwlock) == 0);
  46.         sleep(2);
  47.     }
  48.     pthread_exit(NULL);
  49. }

  50. /*
  51.  * 线程函数 写 减少
  52.  */
  53. void * threadConsume(void * arg)
  54. {
  55.     int cnt = 0;
  56.     while(cnt++ < 100)
  57.     {
  58. //        assert( pthread_mutex_lock(&mutex) == 0);
  59.         assert( pthread_rwlock_wrlock(&rwlock) == 0);
  60.         
  61.         product--;
  62.         printf("Consume: product = %d\n", product);
  63.         
  64. //        assert( pthread_mutex_unlock(&mutex) == 0);
  65.         assert( pthread_rwlock_unlock(&rwlock) == 0);
  66.         sleep(2);
  67.     }
  68.     pthread_exit(NULL);
  69. }    

  70. int main(void)
  71. {
  72.     pthread_t tidRead[20], tidProduce, tidConsume;
  73.     int i;
  74.     
  75.     for(i=0; i<20; i++)
  76.         if( pthread_create(&tidRead[i], NULL,
  77.                         threadRead, NULL) )
  78.         {
  79.             printf("pthread_create ERROR\n");
  80.             exit(-1);
  81.         }
  82.     
  83.     if( pthread_create(&tidProduce, NULL,
  84.                     threadProduce, NULL) )
  85.     {
  86.         printf("pthread_create ERROR\n");
  87.         exit(-1);
  88.     }
  89.     
  90.     if( pthread_create(&tidConsume, NULL,
  91.                     threadConsume, NULL) )
  92.     {
  93.         printf("pthread_create ERROR\n");
  94.         exit(-1);
  95.     }
  96.     
  97.     pthread_exit(NULL);
  98. }
阅读(830) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~