歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Linux多線程──生產者消費者

Linux多線程──生產者消費者

日期:2017/3/1 10:17:06   编辑:Linux編程

生產者消費者問題

這是一個非常經典的多線程題目,題目大意如下:有一個生產者在生產產品,這些產品將提供給若干個消費者去消費,為了使生產者和消費者能並發執行,在兩者之間設置一個有多個緩沖區的緩沖池,生產者將它生產的產品放入一個緩沖區中,消費者可以從緩沖區中取走產品進行消費,所有生產者和消費者都是異步方式運行的,但它們必須保持同步,即不允許消費者到一個空的緩沖區中取產品,也不允許生產者向一個已經裝滿產品且尚未被取走的緩沖區中投放產品。

程序:

  1. // producer_consumer.cpp
  2. //////////////////////////////////////////////////////////////////////
  3. // 有一個生產者在生產產品,這些產品將提供給若干個消費者去消費,為了使生產者和消費者能並發執行,
  4. // 在兩者之間設置一個有多個緩沖區的緩沖池,生產者將它生產的產品放入一個緩沖區中,消費者可以從緩
  5. // 沖區中取走產品進行消費,所有生產者和消費者都是異步方式運行的,但它們必須保持同步,即不允許消
  6. // 費者到一個空的緩沖區中取產品,也不允許生產者向一個已經裝滿產品且尚未被取走的緩沖區中投放產品。
  7. //////////////////////////////////////////////////////////////////////
  8. #include <pthread.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <unistd.h>
  12. const int BUFFER_LENGTH = 100;
  13. int buffer[BUFFER_LENGTH];
  14. int front = 0, rear = -1; // 緩沖區的前端和尾端
  15. int size = 0;
  16. pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  17. pthread_cond_t empty_cond = PTHREAD_COND_INITIALIZER;
  18. pthread_cond_t full_cond = PTHREAD_COND_INITIALIZER;
  19. bool producer_wait = false;
  20. bool consumer_wait = false;
  21. void *producer(void *arg);
  22. void *consumer(void *arg);
  23. int main(int argc, char **argv)
  24. {
  25. pthread_t producer_id;
  26. pthread_t consumer_id;
  27. pthread_create(&producer_id, NULL, producer, NULL);
  28. pthread_create(&consumer_id, NULL, consumer, NULL);
  29. sleep(1);
  30. return 0;
  31. }
  32. void *producer(void *arg)
  33. {
  34. pthread_detach(pthread_self());
  35. while (true)
  36. {
  37. pthread_mutex_lock(&mutex);
  38. if (size == BUFFER_LENGTH) // 如果緩沖區已滿,等待; 否則,添加新產品
  39. {
  40. printf("buffer is full. producer is waiting...\n");
  41. producer_wait = true;
  42. pthread_cond_wait(&full_cond, &mutex);
  43. producer_wait = false;
  44. }
  45. // 往尾端添加一個產品
  46. rear = (rear + 1) % BUFFER_LENGTH;
  47. buffer[rear] = rand() % BUFFER_LENGTH;
  48. printf("producer produces the item %d: %d\n", rear, buffer[rear]);
  49. ++size;
  50. if (size == 1) // 如果當前size=1, 說明以前size=0, 消費者在等待,則給消費者發信號
  51. {
  52. while (true)
  53. {
  54. if (consumer_wait)
  55. {
  56. pthread_cond_signal(&empty_cond);
  57. break;
  58. }
  59. }
  60. }
  61. pthread_mutex_unlock(&mutex);
  62. }
  63. }
  64. void *consumer(void *arg)
  65. {
  66. pthread_detach(pthread_self());
  67. while (true)
  68. {
  69. pthread_mutex_lock(&mutex);
  70. if (size == 0) // 如果緩沖區已空,等待; 否則,消費產品
  71. {
  72. printf("buffer is empty. consumer is waiting...\n");
  73. consumer_wait = true;
  74. pthread_cond_wait(&empty_cond, &mutex);
  75. consumer_wait = false;
  76. }
  77. // 從前端消費一個產品
  78. printf("consumer consumes an item%d: %d\n", front, buffer[front]);
  79. front = (front + 1) % BUFFER_LENGTH;
  80. --size;
  81. if (size == BUFFER_LENGTH-1) // 如果當前size=BUFFER_LENGTH-1,www.linuxidc.com說明以前生產者在等待,則給生產者發信號
  82. {
  83. while (true)
  84. {
  85. if (producer_wait)
  86. {
  87. pthread_cond_signal(&full_cond);
  88. break;
  89. }
  90. }
  91. }
  92. pthread_mutex_unlock(&mutex);
  93. }
  94. }
Copyright © Linux教程網 All Rights Reserved