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

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