[Linux]生产者与消费者 三种模型 C

来源:互联网 发布:淘宝负责人电话号码 编辑:程序博客网 时间:2024/06/02 15:51

“生产者/消费者”问题描述:

有一个有限缓冲区和两个线程:生产者和消费者。他们分别把产品放入缓冲区和从缓冲区中拿走产品。当一个生产者在缓冲区满时必须等待,当一个消费者在缓冲区空时也必须等待。


1.      单锁模型


[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include"stdio.h"  
  2. #include"pthread.h"  
  3.   
  4. int buffer[10];  
  5. int top = 0;  
  6. int itime = 0;  
  7. int itime2 = 0;  
  8.   
  9. pthread_t thread[2];  
  10. pthread_mutex_t mut;  
  11.   
  12. void producer()  
  13. {  
  14.     while(1)  
  15.     {  
  16.         if(itime == 10) return;  
  17.         pthread_mutex_lock(&mut);  
  18.         if(top == 10)  
  19.         {  
  20.             printf("buffer is full...producer is waiting...\n");  
  21.             pthread_mutex_unlock(&mut);  
  22.             continue;  
  23.         }  
  24.         printf("pruducter set the %d\n", itime);  
  25.         top++;  
  26.         itime++;  
  27.         pthread_mutex_unlock(&mut);  
  28.     }  
  29. }  
  30.   
  31.   
  32. void consumer()  
  33. {  
  34.     while(1)  
  35.     {  
  36.         if(itime2 == 10) return;  
  37.         pthread_mutex_lock(&mut);  
  38.         if(top == 0)  
  39.         {  
  40.             printf("buffer is empty...consumer is waiting...\n");  
  41.             pthread_mutex_unlock(&mut);  
  42.             continue;  
  43.         }  
  44.         printf("consumer get the %d\n", itime2);  
  45.         top--;  
  46.         itime2++;  
  47.         pthread_mutex_unlock(&mut);  
  48.     }  
  49. }  
  50.   
  51. int main()  
  52. {  
  53.     pthread_create(&thread[0], NULL, (void*)(&producer), NULL);   
  54.     pthread_create(&thread[1], NULL, (void*)(&consumer), NULL);   
  55.       
  56.     sleep(1);  
  57.     return 0;  
  58. }  


运行结果

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. buffer is empty...consumer is waiting...  
  2. buffer is empty...consumer is waiting...  
  3. buffer is empty...consumer is waiting...  
  4. pruducter set the 0  
  5. pruducter set the 1  
  6. pruducter set the 2  
  7. pruducter set the 3  
  8. pruducter set the 4  
  9. pruducter set the 5  
  10. pruducter set the 6  
  11. pruducter set the 7  
  12. pruducter set the 8  
  13. pruducter set the 9  
  14. consumer get the 0  
  15. consumer get the 1  
  16. consumer get the 2  
  17. consumer get the 3  
  18. consumer get the 4  
  19. consumer get the 5  
  20. consumer get the 6  
  21. consumer get the 7  
  22. consumer get the 8  
  23. consumer get the 9  

分析

容易出现极端状况,一开始一直是consumer加锁,producer无法提供;不久后producer终于得到加锁,存入了10个货物,而后consumer取得10个货物。


 2. 两个互斥锁



[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include"stdio.h"  
  2. #include"pthread.h"  
  3.   
  4. int buffer[10];  
  5. int top = 0;  
  6. int itime = 0;  
  7. int itime2 = 0;  
  8.   
  9. pthread_t thread[2];  
  10. pthread_mutex_t mut;  
  11. pthread_mutex_t mut2;  
  12.   
  13. void producer()  
  14. {  
  15.     while(1)  
  16.     {  
  17.         if(itime == 10) return;  
  18.         pthread_mutex_lock(&mut);  
  19.         if(top == 10)  
  20.         {  
  21.             printf("buffer is full...producer is waiting...\n");  
  22.             pthread_mutex_unlock(&mut2);  
  23.             continue;  
  24.         }  
  25.         printf("pruducter set the %d\n", itime);  
  26.         top++;  
  27.         itime++;  
  28.         pthread_mutex_unlock(&mut2);  
  29.     }  
  30. }  
  31.   
  32.   
  33. void consumer()  
  34. {  
  35.     while(1)  
  36.     {  
  37.         if(itime2 == 10) return;  
  38.         pthread_mutex_lock(&mut2);  
  39.         if(top == 0)  
  40.         {  
  41.             printf("buffer is empty...consumer is waiting...\n");  
  42.             pthread_mutex_unlock(&mut);  
  43.             continue;  
  44.         }  
  45.         printf("consumer get the %d\n", itime2);  
  46.         top--;  
  47.         itime2++;  
  48.         pthread_mutex_unlock(&mut);  
  49.     }  
  50. }  
  51.   
  52. int main()  
  53. {  
  54.     pthread_create(&thread[0], NULL, (void*)(&producer), NULL);   
  55.     pthread_create(&thread[1], NULL, (void*)(&consumer), NULL);   
  56.       
  57.     sleep(1);  
  58.     return 0;  
  59. }  

运行结果

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. buffer is empty...consumer is waiting...  
  2. pruducter set the 0  
  3. consumer get the 0  
  4. pruducter set the 1  
  5. consumer get the 1  
  6. pruducter set the 2  
  7. consumer get the 2  
  8. pruducter set the 3  
  9. consumer get the 3  
  10. pruducter set the 4  
  11. consumer get the 4  
  12. pruducter set the 5  
  13. consumer get the 5  
  14. pruducter set the 6  
  15. consumer get the 6  
  16. pruducter set the 7  
  17. consumer get the 7  
  18. pruducter set the 8  
  19. consumer get the 8  
  20. pruducter set the 9  
  21. consumer get the 9  

很完美对不对?生产者生产一个就把自己锁起来,消费者消费一个后解锁生产者,把自己锁起来,生产者继续生产,循环反复。互斥锁的确能很好的实现进程/线程之间的同步问题,但是它是通过锁机制来实现的,就是仅仅通过加锁和解锁实现同步,效率比较低。


3. 利用条件变量

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include"stdio.h"  
  2. #include"pthread.h"  
  3.   
  4. int buffer[10];  
  5. int top = 0;  
  6. int itime = 0;  
  7. int itime2 = 0;  
  8.   
  9. pthread_t thread[2];  
  10. pthread_mutex_t mut;  
  11. pthread_cond_t con, con2;  
  12.   
  13. void producer()  
  14. {  
  15.     while(1)  
  16.     {  
  17.         if(itime == 10) return;  
  18.         pthread_mutex_lock(&mut);  
  19.         if(top == 10)  
  20.         {  
  21.             printf("buffer is full...producer is waiting...\n");  
  22.             pthread_cond_wait(&con, &mut);  
  23.         }  
  24.         printf("pruducter set the %d\n", itime);  
  25.         top++;  
  26.         itime++;  
  27.         pthread_cond_signal(&con2);  
  28.         pthread_mutex_unlock(&mut);  
  29.         sleep(1);  
  30.     }  
  31. }  
  32.   
  33.   
  34. void consumer()  
  35. {  
  36.     while(1)  
  37.     {  
  38.         if(itime2 == 10) return;  
  39.         pthread_mutex_lock(&mut);  
  40.         if(top == 0)  
  41.         {  
  42.             printf("buffer is empty...consumer is waiting...\n");  
  43.             pthread_cond_wait(&con2, &mut);  
  44.         }  
  45.         printf("consumer get the %d\n", itime2);  
  46.         top--;  
  47.         itime2++;  
  48.         pthread_cond_signal(&con);  
  49.         pthread_mutex_unlock(&mut);  
  50.         sleep(1);  
  51.     }  
  52. }  
  53.   
  54. int main()  
  55. {  
  56.     pthread_create(&thread[0], NULL, (void*)(&producer), NULL);   
  57.     pthread_create(&thread[1], NULL, (void*)(&consumer), NULL);   
  58.       
  59.     sleep(10);  
  60.     return 0;  
  61. }  


运行结果

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. buffer is empty...consumer is waiting...  
  2. pruducter set the 0  
  3. consumer get the 0  
  4. buffer is empty...consumer is waiting...  
  5. pruducter set the 1  
  6. consumer get the 1  
  7. buffer is empty...consumer is waiting...  
  8. pruducter set the 2  
  9. consumer get the 2  
  10. buffer is empty...consumer is waiting...  
  11. pruducter set the 3  
  12. consumer get the 3  
  13. pruducter set the 4  
  14. consumer get the 4  
  15. pruducter set the 5  
  16. consumer get the 5  
  17. pruducter set the 6  
  18. consumer get the 6  
  19. pruducter set the 7  
  20. consumer get the 7  
  21. pruducter set the 8  
  22. consumer get the 8  
  23. pruducter set the 9  
  24. consumer get the 9  


结果还算比较正常,理解一下变量的使用。消费者发现缓冲区没有东西,通过条件变量把自己锁住;生产者生产并激活消费者;消费者从缓冲区消费;当生产者发现缓冲区满的时候,通过条件变量把自己锁住,消费者消费并重新激活生产者。如此。

0 0
原创粉丝点击