Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <asio.hpp>
- #include <iostream>
- #include <atomic>
- //⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠟⠛⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
- //⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⠄⠄⠄⠄⠹⢿⣿⣿⣿⣿⣿⣿⣿⣿
- //⣿⣿⣿⣿⣿⣿⣿⡟⠄⠄⠄⢀⡀⠄⠄⠄⢹⣿⣿⣿⣿⣿⣿⣿
- //⣿⣿⣿⣿⣿⣿⣿⣧⡀⠄⠄⢸⡇⠄⠄⢀⣼⣿⣿⣿⣿⣿⣿⣿
- //⣿⣿⣿⣿⠛⠉⠛⢿⣿⣷⠄⢸⡇⠄⢸⣿⡿⠛⠉⠛⣿⣿⣿⣿
- //⣿⡿⠟⠃⠄⠄⠄⠄⠛⠛⠄⢸⡇⠄⠘⠛⠄⠄⠄⠄⠘⠻⢿⣿
- //⣿⠁⠄⠄⠄⢠⣤⣤⣤⣤⣤⣼⣧⣤⣤⣤⣤⣤⡄⠄⠄⠄⠈⣿
- //⣿⣦⣀⠄⠄⠄⠄⠄⣀⣀⠄⢸⡇⠄⢀⣀⠄⠄⠄⠄⠄⣀⣴⣿
- //⣿⣿⣿⣧⡀⠄⠄⣠⣿⣿⠄⢸⡇⠄⢸⣿⣄⠄⠄⢀⣼⣿⣿⣿
- //⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠄⢸⡇⠄⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿
- //⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠄⢸⡇⠄⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿
- //⣿⣿⣿⣿⣿⣿⣿⠏⠉⠻⠄⢸⡇⠄⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿
- //⣿⣿⣿⣿⣿⡿⠃⠄⢤⡀⠄⢸⡇⠄⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿
- //⣿⣿⣿⣿⣿⣿⣦⣄⠄⠙⠳⣼⡇⠄⠸⣿⣿⣿⣿⣿⣿⣿⣿⣿
- //⣿⣿⣿⣿⣿⣿⣿⣿⣷⣤⠄⢸⡟⢦⣄⠄⠙⠿⣿⣿⣿⣿⣿⣿
- //⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠄⢸⡇⠄⠉⠓⠄⢀⣼⣿⣿⣿⣿⣿
- //⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠄⢸⡇⠄⢠⣀⣠⣿⣿⣿⣿⣿⣿⣿
- //⣿⣿⣿⣿⣿⣿⣿⣿⠟⠛⠄⢸⡇⠄⠘⠻⣿⣿⣿⣿⣿⣿⣿⣿
- //⣿⣿⣿⣿⣿⣿⣿⡇⠄⠄⠄⠸⠇⠄⠄⠄⢸⣿⣿⣿⣿⣿⣿⣿
- //⣿⣿⣿⣿⣿⣿⣿⣷⣄⡀⠄⠄⠄⠄⢀⣠⣾⣿⣿⣿⣿⣿⣿⣿
- //⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣄⠄⠄⣀⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿
- //⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
- asio::awaitable<void> reader()
- {
- auto exec = co_await asio::this_coro::executor;
- asio::steady_timer timer{ exec };
- timer.expires_after(std::chrono::seconds(2));
- asio::error_code ec;
- co_await timer.async_wait(asio::redirect_error(asio::use_awaitable,ec));
- std::cout << "Timer dead\n";
- }
- template <typename Handler>
- class executor_attached_handler
- {
- public:
- explicit executor_attached_handler(asio::any_io_executor exec,Handler&& h): m_exec{exec}, m_handle{std::forward<Handler>(h)}
- {}
- template <typename ...Args>
- void operator()(Args&&... args)
- {
- auto tp = std::tuple<Args...>(std::forward<Args>(args)...);
- asio::dispatch(m_exec, [h = std::move(m_handle),t = std::move(tp)]()
- {
- std::apply(std::move(h), std::move(t));
- });
- }
- private:
- asio::any_io_executor m_exec;
- Handler m_handle;
- };
- template <typename Handler>
- auto bind_executor(asio::any_io_executor ex, Handler h)
- {
- return executor_attached_handler<Handler>{std::move(ex), std::move(h)};
- }
- int main()
- {
- std::cout << "main thread : " << std::this_thread::get_id() << '\n';
- asio::thread_pool pool{ 3 };
- auto strand = asio::make_strand(pool);
- auto handle = []() {std::cout << "Handle thread : " << std::this_thread::get_id() << '\n'; };
- auto mgr = bind_executor(strand, handle);
- mgr();
- pool.join();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement