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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. //如果使用move_only_type来操作channel失败,说明中间过程发生了拷贝操作----这不是设计目标。
  14. template<class _Ty>
  15. struct move_only_type
  16. {
  17. _Ty value;
  18. move_only_type() = default;
  19. move_only_type(const _Ty& val) : value(val) {}
  20. move_only_type(_Ty&& val) : value(std::forward<_Ty>(val)) {}
  21. move_only_type(const move_only_type&) = delete;
  22. move_only_type& operator =(const move_only_type&) = delete;
  23. move_only_type(move_only_type&&) = default;
  24. move_only_type& operator =(move_only_type&&) = default;
  25. };
  26. //如果channel缓存的元素不能凭空产生,或者产生代价较大,则推荐第二个模板参数使用true。从而减小不必要的开销。
  27. using string_channel_t = channel_t<move_only_type<std::string>, false, true>;
  28. //channel其实内部引用了一个channel实现体,故可以支持复制拷贝操作
  29. future_t<> test_channel_read(string_channel_t c)
  30. {
  31. using namespace std::chrono;
  32. for (size_t i = 0; i < 10; ++i)
  33. {
  34. #ifndef __clang__
  35. try
  36. #endif
  37. {
  38. //auto val = co_await c.read();
  39. auto val = co_await c; //第二种从channel读出数据的方法。利用重载operator co_await(),而不是c是一个awaitable_t。
  40. std::cout << val.value << ":";
  41. std::cout << std::endl;
  42. }
  43. #ifndef __clang__
  44. catch (resumef::channel_exception& e)
  45. {
  46. //MAX_CHANNEL_QUEUE=0,并且先读后写,会触发read_before_write异常
  47. std::cout << e.what() << std::endl;
  48. }
  49. #endif
  50. co_await sleep_for(50ms);
  51. }
  52. }
  53. future_t<> test_channel_write(string_channel_t c)
  54. {
  55. using namespace std::chrono;
  56. for (size_t i = 0; i < 10; ++i)
  57. {
  58. //co_await c.write(std::to_string(i));
  59. co_await(c << std::to_string(i)); //第二种写入数据到channel的方法。因为优先级关系,需要将'c << i'括起来
  60. std::cout << "<" << i << ">:";
  61. std::cout << std::endl;
  62. }
  63. }
  64. void test_channel_read_first()
  65. {
  66. string_channel_t c(MAX_CHANNEL_QUEUE);
  67. go test_channel_read(c);
  68. go test_channel_write(c);
  69. this_scheduler()->run_until_notask();
  70. }
  71. void test_channel_write_first()
  72. {
  73. string_channel_t c(MAX_CHANNEL_QUEUE);
  74. go test_channel_write(c);
  75. go test_channel_read(c);
  76. this_scheduler()->run_until_notask();
  77. }
  78. static const int N = 1000000;
  79. void test_channel_performance(size_t buff_size)
  80. {
  81. //1的话,效率跟golang比,有点惨不忍睹。
  82. //1000的话,由于几乎不需要调度器接入,效率就很高了,随便过千万数量级。
  83. channel_t<int, false, true> c{ buff_size };
  84. go[&]() -> future_t<>
  85. {
  86. for (int i = N - 1; i >= 0; --i)
  87. {
  88. co_await(c << i);
  89. }
  90. };
  91. go[&]() -> future_t<>
  92. {
  93. auto tstart = high_resolution_clock::now();
  94. int i;
  95. do
  96. {
  97. i = co_await c;
  98. } while (i > 0);
  99. auto dt = duration_cast<duration<double>>(high_resolution_clock::now() - tstart).count();
  100. std::cout << "channel buff=" << c.capacity() << ", w/r " << N << " times, cost time " << dt << "s" << std::endl;
  101. };
  102. this_scheduler()->run_until_notask();
  103. }
  104. void resumable_main_channel()
  105. {
  106. test_channel_read_first();
  107. std::cout << std::endl;
  108. test_channel_write_first();
  109. std::cout << std::endl;
  110. test_channel_performance(1);
  111. test_channel_performance(10);
  112. test_channel_performance(100);
  113. test_channel_performance(1000);
  114. test_channel_performance(10000);
  115. }