基于C++ Coroutines提案 ‘Stackless Resumable Functions’编写的协程库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_async_channel_mult_thread.cpp 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //验证channel是否线程安全
  2. #include <chrono>
  3. #include <iostream>
  4. #include <string>
  5. #include <conio.h>
  6. #include <thread>
  7. #include <deque>
  8. #include <mutex>
  9. #include "librf.h"
  10. using namespace resumef;
  11. using namespace std::chrono;
  12. static std::mutex cout_mutex;
  13. std::atomic<intptr_t> gcounter = 0;
  14. #define OUTPUT_DEBUG 0
  15. future_t<> test_channel_consumer(const channel_t<std::string> & c, size_t cnt)
  16. {
  17. for (size_t i = 0; i < cnt; ++i)
  18. {
  19. #ifndef __clang__
  20. try
  21. #endif
  22. {
  23. auto val = co_await c.read();
  24. ++gcounter;
  25. #if OUTPUT_DEBUG
  26. {
  27. scoped_lock<std::mutex> __lock(cout_mutex);
  28. std::cout << "R " << val << "@" << std::this_thread::get_id() << std::endl;
  29. }
  30. #endif
  31. }
  32. #ifndef __clang__
  33. catch (channel_exception& e)
  34. {
  35. //MAX_CHANNEL_QUEUE=0,并且先读后写,会触发read_before_write异常
  36. scoped_lock<std::mutex> __lock(cout_mutex);
  37. std::cout << e.what() << std::endl;
  38. }
  39. #endif
  40. #if OUTPUT_DEBUG
  41. co_await sleep_for(50ms);
  42. #endif
  43. }
  44. }
  45. future_t<> test_channel_producer(const channel_t<std::string> & c, size_t cnt)
  46. {
  47. for (size_t i = 0; i < cnt; ++i)
  48. {
  49. co_await c.write(std::to_string(i));
  50. #if OUTPUT_DEBUG
  51. {
  52. scoped_lock<std::mutex> __lock(cout_mutex);
  53. std::cout << "W " << i << "@" << std::this_thread::get_id() << std::endl;
  54. }
  55. #endif
  56. }
  57. }
  58. const size_t WRITE_THREAD = 6;
  59. const size_t READ_THREAD = 6;
  60. const size_t READ_BATCH = 1000000;
  61. const size_t MAX_CHANNEL_QUEUE = 5; //0, 1, 5, 10, -1
  62. void resumable_main_channel_mult_thread()
  63. {
  64. channel_t<std::string> c(MAX_CHANNEL_QUEUE);
  65. std::thread write_th[WRITE_THREAD];
  66. for (size_t i = 0; i < WRITE_THREAD; ++i)
  67. {
  68. write_th[i] = std::thread([&]
  69. {
  70. local_scheduler my_scheduler;
  71. go test_channel_producer(c, READ_BATCH * READ_THREAD / WRITE_THREAD);
  72. #if RESUMEF_ENABLE_MULT_SCHEDULER
  73. this_scheduler()->run_until_notask();
  74. #endif
  75. {
  76. scoped_lock<std::mutex> __lock(cout_mutex);
  77. std::cout << "Write OK\r\n";
  78. }
  79. });
  80. }
  81. std::this_thread::sleep_for(100ms);
  82. std::thread read_th[READ_THREAD];
  83. for (size_t i = 0; i < READ_THREAD; ++i)
  84. {
  85. read_th[i] = std::thread([&]
  86. {
  87. local_scheduler my_scheduler;
  88. go test_channel_consumer(c, READ_BATCH);
  89. #if RESUMEF_ENABLE_MULT_SCHEDULER
  90. this_scheduler()->run_until_notask();
  91. #endif
  92. {
  93. scoped_lock<std::mutex> __lock(cout_mutex);
  94. std::cout << "Read OK\r\n";
  95. }
  96. });
  97. }
  98. #if !RESUMEF_ENABLE_MULT_SCHEDULER
  99. std::this_thread::sleep_for(100ms);
  100. scheduler_t::g_scheduler.run_until_notask();
  101. #endif
  102. for(auto & th : read_th)
  103. th.join();
  104. for (auto& th : write_th)
  105. th.join();
  106. std::cout << "OK: counter = " << gcounter.load() << std::endl;
  107. }