producer_consumer.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // (C) Copyright 2012 Howard Hinnant
  2. // (C) Copyright 2012 Vicente Botet
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. // adapted from the example given by Howard Hinnant in
  7. #include <boost/config.hpp>
  8. #define BOOST_THREAD_VERSION 4
  9. #define BOOST_THREAD_QUEUE_DEPRECATE_OLD
  10. #if ! defined BOOST_NO_CXX11_DECLTYPE
  11. #define BOOST_RESULT_OF_USE_DECLTYPE
  12. #endif
  13. #include <iostream>
  14. #include <boost/thread/scoped_thread.hpp>
  15. #ifdef XXXX
  16. #include <boost/thread/externally_locked_stream.hpp>
  17. typedef boost::externally_locked_stream<std::ostream> the_ostream;
  18. #else
  19. typedef std::ostream the_ostream;
  20. typedef std::istream the_istream;
  21. #endif
  22. #include <boost/thread/concurrent_queues/sync_queue.hpp>
  23. void producer(the_ostream & /*mos*/, boost::sync_queue<int> & sbq)
  24. {
  25. using namespace boost;
  26. try {
  27. for(int i=0; ;++i)
  28. {
  29. sbq.push(i);
  30. //sbq << i;
  31. //mos << "push(" << i << ") "<< sbq.size()<<"\n";
  32. this_thread::sleep_for(chrono::milliseconds(200));
  33. }
  34. }
  35. catch(sync_queue_is_closed&)
  36. {
  37. //mos << "closed !!!\n";
  38. }
  39. catch(...)
  40. {
  41. //mos << "exception !!!\n";
  42. }
  43. }
  44. void consumer(
  45. the_ostream & /*mos*/,
  46. boost::sync_queue<int> & sbq)
  47. {
  48. using namespace boost;
  49. try {
  50. for(int i=0; ;++i)
  51. {
  52. int r;
  53. sbq.pull(r);
  54. //sbq >> r;
  55. //mos << i << " pull(" << r << ") "<< sbq.size()<<"\n";
  56. this_thread::sleep_for(chrono::milliseconds(250));
  57. }
  58. }
  59. catch(sync_queue_is_closed&)
  60. {
  61. //mos << "closed !!!\n";
  62. }
  63. catch(...)
  64. {
  65. //mos << "exception !!!\n";
  66. }
  67. }
  68. void consumer2(the_ostream &/*mos*/, boost::sync_queue<int> & sbq)
  69. {
  70. using namespace boost;
  71. try {
  72. for(int i=0; ;++i)
  73. {
  74. int r;
  75. queue_op_status st = sbq.try_pull(r);
  76. if (queue_op_status::closed == st) break;
  77. if (queue_op_status::success == st) {
  78. //mos << i << " pull(" << r << ")\n";
  79. }
  80. this_thread::sleep_for(chrono::milliseconds(250));
  81. }
  82. }
  83. catch(...)
  84. {
  85. //mos << "exception !!!\n";
  86. }
  87. }
  88. void consumer3(the_ostream &/*mos*/, boost::sync_queue<int> & sbq)
  89. {
  90. using namespace boost;
  91. try {
  92. for(int i=0; ;++i)
  93. {
  94. int r;
  95. queue_op_status res = sbq.wait_pull(r);
  96. if (res==queue_op_status::closed) break;
  97. //mos << i << " wait_pull(" << r << ")\n";
  98. this_thread::sleep_for(chrono::milliseconds(250));
  99. }
  100. }
  101. catch(...)
  102. {
  103. //mos << "exception !!!\n";
  104. }
  105. }
  106. int main()
  107. {
  108. using namespace boost;
  109. #ifdef XXXX
  110. recursive_mutex terminal_mutex;
  111. externally_locked_stream<std::ostream> mcerr(std::cerr, terminal_mutex);
  112. externally_locked_stream<std::ostream> mcout(std::cout, terminal_mutex);
  113. externally_locked_stream<std::istream> mcin(std::cin, terminal_mutex);
  114. #else
  115. the_ostream &mcerr = std::cout;
  116. the_ostream &mcout = std::cerr;
  117. //the_istream &mcin = std::cin;
  118. #endif
  119. sync_queue<int> sbq;
  120. {
  121. mcout << "begin of main" << std::endl;
  122. scoped_thread<> t11(boost::thread(producer, boost::ref(mcerr), boost::ref(sbq)));
  123. scoped_thread<> t12(boost::thread(producer, boost::ref(mcerr), boost::ref(sbq)));
  124. scoped_thread<> t2(boost::thread(consumer, boost::ref(mcout), boost::ref(sbq)));
  125. this_thread::sleep_for(chrono::seconds(1));
  126. mcout << "closed()" << std::endl;
  127. sbq.close();
  128. mcout << "closed()" << std::endl;
  129. } // all threads joined here.
  130. mcout << "end of main" << std::endl;
  131. return 0;
  132. }