基于C++ Coroutines提案 ‘Stackless Resumable Functions’编写的协程库
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

test_async_channel_mult_thread.cpp 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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_vt test_channel_consumer(const channel_t<std::string> & c, size_t cnt)
  16. {
  17. for (size_t i = 0; i < cnt; ++i)
  18. {
  19. try
  20. {
  21. auto val = co_await c.read();
  22. ++gcounter;
  23. #if OUTPUT_DEBUG
  24. {
  25. scoped_lock<std::mutex> __lock(cout_mutex);
  26. std::cout << "R " << val << "@" << std::this_thread::get_id() << std::endl;
  27. }
  28. #endif
  29. }
  30. catch (channel_exception e)
  31. {
  32. //MAX_CHANNEL_QUEUE=0,并且先读后写,会触发read_before_write异常
  33. scoped_lock<std::mutex> __lock(cout_mutex);
  34. std::cout << e.what() << std::endl;
  35. }
  36. #if OUTPUT_DEBUG
  37. co_await sleep_for(50ms);
  38. #endif
  39. }
  40. }
  41. future_vt test_channel_producer(const channel_t<std::string> & c, size_t cnt)
  42. {
  43. for (size_t i = 0; i < cnt; ++i)
  44. {
  45. co_await c.write(std::to_string(i));
  46. #if OUTPUT_DEBUG
  47. {
  48. scoped_lock<std::mutex> __lock(cout_mutex);
  49. std::cout << "W " << i << "@" << std::this_thread::get_id() << std::endl;
  50. }
  51. #endif
  52. }
  53. }
  54. const size_t N = 8;
  55. const size_t BATCH = 1000000;
  56. const size_t MAX_CHANNEL_QUEUE = N + 1; //0, 1, 5, 10, -1
  57. void resumable_main_channel_mult_thread()
  58. {
  59. channel_t<std::string> c(MAX_CHANNEL_QUEUE);
  60. std::thread write_th([&]
  61. {
  62. local_scheduler my_scheduler; //2017/12/14日,仍然存在BUG。真多线程下调度,存在有协程无法被调度完成的BUG
  63. go test_channel_producer(c, BATCH * N);
  64. #if RESUMEF_ENABLE_MULT_SCHEDULER
  65. this_scheduler()->run_until_notask();
  66. #endif
  67. std::cout << "Write OK\r\n";
  68. });
  69. //std::this_thread::sleep_for(100ms);
  70. std::thread read_th[N];
  71. for (size_t i = 0; i < N; ++i)
  72. {
  73. read_th[i] = std::thread([&]
  74. {
  75. local_scheduler my_scheduler; //2017/12/14日,仍然存在BUG。真多线程下调度,存在有协程无法被调度完成的BUG
  76. go test_channel_consumer(c, BATCH);
  77. #if RESUMEF_ENABLE_MULT_SCHEDULER
  78. this_scheduler()->run_until_notask();
  79. #endif
  80. std::cout << "Read OK\r\n";
  81. });
  82. }
  83. #if !RESUMEF_ENABLE_MULT_SCHEDULER
  84. std::this_thread::sleep_for(100ms);
  85. scheduler::g_scheduler.run_until_notask();
  86. #endif
  87. for(auto & th : read_th)
  88. th.join();
  89. write_th.join();
  90. std::cout << "OK: counter = " << gcounter.load() << std::endl;
  91. (void)_getch();
  92. }