基于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.cpp 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. 
  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. const size_t MAX_CHANNEL_QUEUE = 5; //0, 1, 5, 10, -1
  13. future_t<> test_channel_read(const channel_t<std::string> & c)
  14. {
  15. using namespace std::chrono;
  16. for (size_t i = 0; i < 10; ++i)
  17. {
  18. #ifndef __clang__
  19. try
  20. #endif
  21. {
  22. auto val = co_await c.read();
  23. //auto val = co_await c; //第二种从channel读出数据的方法。利用重载operator co_await(),而不是c是一个awaitable_t。
  24. std::cout << val << ":";
  25. #if _DEBUG
  26. for (auto v2 : c.debug_queue())
  27. std::cout << v2 << ",";
  28. #endif
  29. std::cout << std::endl;
  30. }
  31. #ifndef __clang__
  32. catch (resumef::channel_exception& e)
  33. {
  34. //MAX_CHANNEL_QUEUE=0,并且先读后写,会触发read_before_write异常
  35. std::cout << e.what() << std::endl;
  36. }
  37. #endif
  38. co_await sleep_for(50ms);
  39. }
  40. }
  41. future_t<> test_channel_write(const channel_t<std::string> & c)
  42. {
  43. using namespace std::chrono;
  44. for (size_t i = 0; i < 10; ++i)
  45. {
  46. co_await c.write(std::to_string(i));
  47. //co_await (c << std::to_string(i)); //第二种写入数据到channel的方法。因为优先级关系,需要将'c << i'括起来
  48. std::cout << "<" << i << ">:";
  49. #if _DEBUG
  50. for (auto val : c.debug_queue())
  51. std::cout << val << ",";
  52. #endif
  53. std::cout << std::endl;
  54. }
  55. }
  56. void test_channel_read_first()
  57. {
  58. channel_t<std::string> c(MAX_CHANNEL_QUEUE);
  59. go test_channel_read(c);
  60. go test_channel_write(c);
  61. this_scheduler()->run_until_notask();
  62. }
  63. void test_channel_write_first()
  64. {
  65. channel_t<std::string> c(MAX_CHANNEL_QUEUE);
  66. go test_channel_write(c);
  67. go test_channel_read(c);
  68. this_scheduler()->run_until_notask();
  69. }
  70. static const int N = 1000000;
  71. void test_channel_performance()
  72. {
  73. channel_t<int> c{1};
  74. go[&]() -> future_t<>
  75. {
  76. for (int i = N - 1; i >= 0; --i)
  77. {
  78. co_await(c << i);
  79. }
  80. };
  81. go[&]() -> future_t<>
  82. {
  83. auto tstart = high_resolution_clock::now();
  84. int i;
  85. do
  86. {
  87. i = co_await c;
  88. } while (i > 0);
  89. auto dt = duration_cast<duration<double>>(high_resolution_clock::now() - tstart).count();
  90. std::cout << "channel w/r " << N << " times, cost time " << dt << "s" << std::endl;
  91. };
  92. this_scheduler()->run_until_notask();
  93. }
  94. void resumable_main_channel()
  95. {
  96. test_channel_read_first();
  97. std::cout << std::endl;
  98. test_channel_write_first();
  99. std::cout << std::endl;
  100. test_channel_performance();
  101. }