基于C++ Coroutines提案 ‘Stackless Resumable Functions’编写的协程库
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

test_async_channel.cpp 2.7KB

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