当前位置:   article > 正文

C++多线程例题-生产者和消费者问题_c++多线程选择题

c++多线程选择题

        生产者每次生产1个随机整数,放到缓冲区中;消费者每次从缓冲区中按生产顺序取出1个整数。每次缓冲区发生变化,打印放入/取出的数,以及缓冲当前大小。

        情形1:初始为空,生产速率2/秒,消费速率1/秒

        情形2:初始全满,生产速率1/秒,消费速率2/秒

        情形3:初始半满,生产速率=消费速率

参考这个博客进行了一些改动

三个输入参数分别是当前的buffer中item的数量,生产者生产速率,消费者消费速率。

  1. #include <mutex>
  2. #include <condition_variable>
  3. #include <windows.h>
  4. #include <thread>
  5. #include<iostream>
  6. using namespace std;
  7. static const int buffer_size = 128; // 缓存大小
  8. static const int item_total = 300; //总共要生产 item_total个item
  9. int produce_num = 1;
  10. int consume_num = 1;
  11. // 缓存结构体, 使用循环队列当做缓存
  12. struct Buffer
  13. {
  14. int buffer[buffer_size];
  15. size_t read_position; // 当前读位置
  16. size_t write_position; // 当前写位置
  17. mutex mtx; // 读写互斥
  18. //条件变量
  19. condition_variable not_full;
  20. condition_variable not_empty;
  21. } buffer_res;
  22. typedef struct Buffer Buffer;
  23. int get_write_position(Buffer *b)
  24. {
  25. for (int i = 0; i < buffer_size; i++)
  26. {
  27. if (0 == b->buffer[i])
  28. {
  29. return i;
  30. }
  31. }
  32. return buffer_size;
  33. }
  34. int get_read_position(Buffer *b)
  35. {
  36. for (int i = 0; i < buffer_size; i++)
  37. {
  38. if (0 != b->buffer[i])
  39. {
  40. return i;
  41. }
  42. }
  43. return buffer_size;
  44. }
  45. void porduce_item(Buffer *b)
  46. {
  47. unique_lock<mutex> lock(b->mtx); //设置互斥锁
  48. while (1)
  49. {
  50. b->write_position = get_write_position(b);
  51. if (b->write_position == buffer_size)
  52. {
  53. //当前缓存已经满了
  54. cout << "buffer is full now, producer is wating....." << endl;
  55. (b->not_full).wait(lock); // 等待缓存非full
  56. }
  57. else
  58. {
  59. break;
  60. }
  61. }
  62. int item = rand() % buffer_size + 1;
  63. cout << "produce item: " << item << " in " << b->write_position << " ***" << endl;
  64. // 向缓存中添加item
  65. (b->buffer)[b->write_position] = item;
  66. (b->not_empty).notify_all();
  67. lock.unlock();
  68. }
  69. int consume_item(Buffer *b)
  70. {
  71. unique_lock<mutex> lock(b->mtx);
  72. while (1)
  73. {
  74. b->read_position = get_read_position(b);
  75. if (b->read_position == buffer_size)
  76. {
  77. // 当前buffer 为空
  78. cout << "buffer is empty , consumer is waiting....." << endl;
  79. (b->not_empty).wait(lock);
  80. }
  81. else
  82. {
  83. break;
  84. }
  85. }
  86. int item = (b->buffer)[b->read_position];
  87. (b->buffer)[b->read_position] = 0;
  88. cout << "consume item: " << item << " in " << b->read_position << endl;
  89. (b->not_full).notify_all();
  90. lock.unlock();
  91. return item;
  92. }
  93. //生产者任务
  94. void producer(int buf)
  95. {
  96. for (int i = 1; i <= item_total - buf; i++)
  97. {
  98. //生产item需要的时间
  99. Sleep(20 / produce_num);
  100. porduce_item(&buffer_res);
  101. }
  102. }
  103. //消费者任务
  104. void consumer()
  105. {
  106. static int cnt = 0;
  107. while (1)
  108. {
  109. //消费需要的时间
  110. Sleep(20 / consume_num);
  111. int item = consume_item(&buffer_res);
  112. if (++cnt == item_total)
  113. break;
  114. }
  115. }
  116. //初始化 buffer, buf空满状态,p_num为produce_num, c_num为consume_num
  117. void init_buffer(Buffer *b, const int buf)
  118. {
  119. for (int i = 0; i < buf; i++)
  120. {
  121. b->buffer[i] = rand() % buffer_size + 1;
  122. }
  123. for (int i = buf; i < buffer_size; i++)
  124. {
  125. b->buffer[i] = 0;
  126. }
  127. }
  128. int main(int argc, char **argv)
  129. {
  130. int buf = 0;
  131. if (argc >= 4)
  132. {
  133. buf = atoi(argv[1]);
  134. produce_num = atoi(argv[2]);
  135. consume_num = atoi(argv[3]);
  136. }
  137. else
  138. {
  139. cout << "Please enter the initial buffer items, "
  140. "produce_num ∈ {1,2} and consume_num ∈ {1,2}."
  141. << endl;
  142. cin >> buf >> produce_num >> consume_num;
  143. }
  144. init_buffer(&buffer_res, buf);
  145. thread produce(producer, buf);
  146. thread consume(consumer);
  147. produce.join();
  148. consume.join();
  149. return 0;
  150. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/267902
推荐阅读
相关标签
  

闽ICP备14008679号