基于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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 THREAD = 12;
  59. const size_t BATCH = 10000;
  60. const size_t MAX_CHANNEL_QUEUE = THREAD + 1; //0, 1, 5, 10, -1
  61. void resumable_main_channel_mult_thread()
  62. {
  63. channel_t<std::string> c(MAX_CHANNEL_QUEUE);
  64. std::thread write_th([&]
  65. {
  66. local_scheduler my_scheduler; //2017/12/14日,仍然存在BUG。真多线程下调度,存在有协程无法被调度完成的BUG
  67. go test_channel_producer(c, BATCH * THREAD);
  68. #if RESUMEF_ENABLE_MULT_SCHEDULER
  69. this_scheduler()->run_until_notask();
  70. #endif
  71. {
  72. scoped_lock<std::mutex> __lock(cout_mutex);
  73. std::cout << "Write OK\r\n";
  74. }
  75. });
  76. std::this_thread::sleep_for(100ms);
  77. std::thread read_th[THREAD];
  78. for (size_t i = 0; i < THREAD; ++i)
  79. {
  80. read_th[i] = std::thread([&]
  81. {
  82. local_scheduler my_scheduler; //2017/12/14日,仍然存在BUG。真多线程下调度,存在有协程无法被调度完成的BUG
  83. go test_channel_consumer(c, BATCH);
  84. #if RESUMEF_ENABLE_MULT_SCHEDULER
  85. this_scheduler()->run_until_notask();
  86. #endif
  87. {
  88. scoped_lock<std::mutex> __lock(cout_mutex);
  89. std::cout << "Read OK\r\n";
  90. }
  91. });
  92. }
  93. #if !RESUMEF_ENABLE_MULT_SCHEDULER
  94. std::this_thread::sleep_for(100ms);
  95. scheduler_t::g_scheduler.run_until_notask();
  96. #endif
  97. for(auto & th : read_th)
  98. th.join();
  99. write_th.join();
  100. std::cout << "OK: counter = " << gcounter.load() << std::endl;
  101. }