Advertisement
f0rmull

Untitled

Mar 26th, 2022
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.01 KB | None | 0 0
  1. #include <asio.hpp>
  2. #include <iostream>
  3. #include <atomic>
  4. //⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠟⠛⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
  5. //⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⠄⠄⠄⠄⠹⢿⣿⣿⣿⣿⣿⣿⣿⣿
  6. //⣿⣿⣿⣿⣿⣿⣿⡟⠄⠄⠄⢀⡀⠄⠄⠄⢹⣿⣿⣿⣿⣿⣿⣿
  7. //⣿⣿⣿⣿⣿⣿⣿⣧⡀⠄⠄⢸⡇⠄⠄⢀⣼⣿⣿⣿⣿⣿⣿⣿
  8. //⣿⣿⣿⣿⠛⠉⠛⢿⣿⣷⠄⢸⡇⠄⢸⣿⡿⠛⠉⠛⣿⣿⣿⣿
  9. //⣿⡿⠟⠃⠄⠄⠄⠄⠛⠛⠄⢸⡇⠄⠘⠛⠄⠄⠄⠄⠘⠻⢿⣿
  10. //⣿⠁⠄⠄⠄⢠⣤⣤⣤⣤⣤⣼⣧⣤⣤⣤⣤⣤⡄⠄⠄⠄⠈⣿
  11. //⣿⣦⣀⠄⠄⠄⠄⠄⣀⣀⠄⢸⡇⠄⢀⣀⠄⠄⠄⠄⠄⣀⣴⣿
  12. //⣿⣿⣿⣧⡀⠄⠄⣠⣿⣿⠄⢸⡇⠄⢸⣿⣄⠄⠄⢀⣼⣿⣿⣿
  13. //⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠄⢸⡇⠄⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿
  14. //⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠄⢸⡇⠄⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿
  15. //⣿⣿⣿⣿⣿⣿⣿⠏⠉⠻⠄⢸⡇⠄⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿
  16. //⣿⣿⣿⣿⣿⡿⠃⠄⢤⡀⠄⢸⡇⠄⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿
  17. //⣿⣿⣿⣿⣿⣿⣦⣄⠄⠙⠳⣼⡇⠄⠸⣿⣿⣿⣿⣿⣿⣿⣿⣿
  18. //⣿⣿⣿⣿⣿⣿⣿⣿⣷⣤⠄⢸⡟⢦⣄⠄⠙⠿⣿⣿⣿⣿⣿⣿
  19. //⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠄⢸⡇⠄⠉⠓⠄⢀⣼⣿⣿⣿⣿⣿
  20. //⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠄⢸⡇⠄⢠⣀⣠⣿⣿⣿⣿⣿⣿⣿
  21. //⣿⣿⣿⣿⣿⣿⣿⣿⠟⠛⠄⢸⡇⠄⠘⠻⣿⣿⣿⣿⣿⣿⣿⣿
  22. //⣿⣿⣿⣿⣿⣿⣿⡇⠄⠄⠄⠸⠇⠄⠄⠄⢸⣿⣿⣿⣿⣿⣿⣿
  23. //⣿⣿⣿⣿⣿⣿⣿⣷⣄⡀⠄⠄⠄⠄⢀⣠⣾⣿⣿⣿⣿⣿⣿⣿
  24. //⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣄⠄⠄⣀⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿
  25. //⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
  26.  
  27.  
  28. asio::awaitable<void> reader()
  29. {
  30.     auto exec = co_await asio::this_coro::executor;
  31.     asio::steady_timer timer{ exec };
  32.  
  33.     timer.expires_after(std::chrono::seconds(2));
  34.     asio::error_code ec;
  35.     co_await timer.async_wait(asio::redirect_error(asio::use_awaitable,ec));
  36.     std::cout << "Timer dead\n";
  37. }
  38.  
  39.  
  40. template <typename Handler>
  41. class executor_attached_handler
  42. {
  43. public:
  44.     explicit executor_attached_handler(asio::any_io_executor exec,Handler&& h): m_exec{exec}, m_handle{std::forward<Handler>(h)}
  45.     {}
  46.  
  47.     template <typename ...Args>
  48.     void operator()(Args&&... args)
  49.     {
  50.         auto tp = std::tuple<Args...>(std::forward<Args>(args)...);
  51.         asio::dispatch(m_exec, [h = std::move(m_handle),t = std::move(tp)]()
  52.             {
  53.                 std::apply(std::move(h), std::move(t));
  54.             });
  55.     }
  56. private:
  57.     asio::any_io_executor m_exec;
  58.     Handler m_handle;
  59. };
  60.  
  61. template <typename Handler>
  62. auto bind_executor(asio::any_io_executor ex, Handler h)
  63. {
  64.     return executor_attached_handler<Handler>{std::move(ex), std::move(h)};
  65.  
  66. }
  67.  
  68. int main()
  69. {
  70.     std::cout << "main thread : " << std::this_thread::get_id() << '\n';
  71.     asio::thread_pool pool{ 3 };
  72.     auto strand = asio::make_strand(pool);
  73.     auto handle = []() {std::cout << "Handle thread : " << std::this_thread::get_id() << '\n'; };
  74.     auto mgr = bind_executor(strand, handle);
  75.     mgr();
  76.  
  77.     pool.join();
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement