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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. //依赖 https://github.com/tearshark/modern_cb 项目
  2. //依赖 https://github.com/tearshark/librf 项目
  3. #include <future>
  4. #include <string>
  5. #include <iostream>
  6. #include "modern_callback.h"
  7. //原旨主义的异步函数,其回调写法大致如下
  8. template<typename _Input_t, typename _Callable_t>
  9. void tostring_async_originalism(_Input_t&& value, _Callable_t&& token)
  10. {
  11. std::thread([callback = std::move(token), value = std::forward<_Input_t>(value)]
  12. {
  13. callback(std::to_string(value));
  14. }).detach();
  15. }
  16. void tostring_async_originalism2(int value, std::function<void(std::string)>&& token, double mu)
  17. {
  18. std::thread([callback = std::move(token), value, mu]
  19. {
  20. callback(std::to_string(value * mu));
  21. }).detach();
  22. }
  23. //使用原旨主义的方式扩展异步方法来支持future
  24. template<typename _Input_t>
  25. auto tostring_async_originalism_future(_Input_t&& value)
  26. {
  27. std::promise<std::string> _promise;
  28. std::future<std::string> _future = _promise.get_future();
  29. std::thread([_promise = std::move(_promise), value = std::forward<_Input_t>(value)]() mutable
  30. {
  31. _promise.set_value(std::to_string(value));
  32. }).detach();
  33. return std::move(_future);
  34. }
  35. //----------------------------------------------------------------------------------------------------------------------
  36. //下面演示如何扩展tostring_async函数,以支持future模式
  37. template<typename _Input_t, typename _Callable_t>
  38. auto tostring_async(_Input_t&& value, _Callable_t&& token)
  39. {
  40. MODERN_CALLBACK_TRAITS(token, void(std::string));
  41. std::thread([callback = MODERN_CALLBACK_CALL(), value = std::forward<_Input_t>(value)]
  42. {
  43. callback(std::to_string(value));
  44. }).detach();
  45. MODERN_CALLBACK_RETURN();
  46. }
  47. //演示异步库有多个异步回调函数,只要按照Modern Callback范式去做回调,就不再需要写额外的代码,就可以适配到future+librf,以及更多的其他库
  48. template<typename _Ty1, typename _Ty2, typename _Callable_t>
  49. auto add_async(_Ty1&& val1, _Ty2&& val2, _Callable_t&& token)
  50. {
  51. MODERN_CALLBACK_TRAITS(token, void(decltype(val1 + val2)));
  52. std::thread([=, callback = MODERN_CALLBACK_CALL()]
  53. {
  54. using namespace std::literals;
  55. std::this_thread::sleep_for(0.1s);
  56. callback(val1 + val2);
  57. }).detach();
  58. MODERN_CALLBACK_RETURN();
  59. }
  60. //演示异步库有多个异步回调函数,只要按照Modern Callback范式去做回调,就不再需要写额外的代码,就可以适配到future+librf,以及更多的其他库
  61. template<typename _Ty1, typename _Ty2, typename _Callable_t>
  62. auto muldiv_async(_Ty1&& val1, _Ty2&& val2, _Callable_t&& token)
  63. {
  64. MODERN_CALLBACK_TRAITS(token, void(std::exception_ptr, decltype(val1 * val2), decltype(val1 / val2)));
  65. std::thread([=, callback = MODERN_CALLBACK_CALL()]
  66. {
  67. using namespace std::literals;
  68. std::this_thread::sleep_for(0.1s);
  69. auto v1 = val1 * val2;
  70. if (val2 == 0)
  71. callback(std::make_exception_ptr(std::logic_error("divided by zero")), v1, 0);
  72. else
  73. callback(nullptr, v1, val1 / val2);
  74. }).detach();
  75. MODERN_CALLBACK_RETURN();
  76. }
  77. #include "use_future.h"
  78. #if TEST_ASYNC_CALLBACK
  79. #include "async_call.hpp"
  80. #endif
  81. static void example_future()
  82. {
  83. using namespace std::literals;
  84. //使用lambda作为异步回调函数,传统用法
  85. tostring_async_originalism(-1.0, [](std::string&& value)
  86. {
  87. std::cout << value << std::endl;
  88. });
  89. std::this_thread::sleep_for(0.5s);
  90. tostring_async(1.0, [](std::string&& value)
  91. {
  92. std::cout << value << std::endl;
  93. });
  94. std::this_thread::sleep_for(0.5s);
  95. std::cout << "......" << std::endl;
  96. //支持future的用法
  97. std::future<std::string> f1 = tostring_async_originalism_future(5);
  98. std::cout << f1.get() << std::endl;
  99. std::future<std::string> f2 = tostring_async(6.0f, std_future);
  100. std::cout << f2.get() << std::endl;
  101. #if TEST_ASYNC_CALLBACK
  102. std::future<std::string> f3 = async_call(&tostring_async_originalism2, 99, placeholder::_cb(std_future), 2.0);
  103. std::cout << f3.get() << std::endl;
  104. #endif
  105. }
  106. #include "librf/librf.h"
  107. #include "use_librf.h"
  108. static void example_librf()
  109. {
  110. //支持librf的用法
  111. GO
  112. {
  113. try
  114. {
  115. int val = co_await add_async(1, 2, use_librf);
  116. std::cout << val << std::endl;
  117. //muldiv_async函数可能会抛异常,取决于val是否是0
  118. //异常将会带回到本协程里的代码,所以需要try-catch
  119. auto [a, b] = co_await muldiv_async(9, val, use_librf);
  120. std::string result = co_await tostring_async(a + b, use_librf);
  121. std::cout << result << std::endl;
  122. #if TEST_ASYNC_CALLBACK
  123. result = co_await async_call(&tostring_async_originalism2, 99, placeholder::_cb(use_librf), 2.0);
  124. std::cout << result << std::endl;
  125. #endif
  126. }
  127. catch (const std::exception & e)
  128. {
  129. std::cout << "exception signal : " << e.what() << std::endl;
  130. }
  131. catch (...)
  132. {
  133. std::cout << "exception signal : who knows?" << std::endl;
  134. }
  135. };
  136. librf::this_scheduler()->run_until_notask();
  137. }
  138. void resumable_main_modern_cb()
  139. {
  140. std::cout << __FUNCTION__ << std::endl;
  141. example_future();
  142. example_librf();
  143. }
  144. #if LIBRF_TUTORIAL_STAND_ALONE
  145. int main()
  146. {
  147. resumable_main_modern_cb();
  148. return 0;
  149. }
  150. #endif