Krzysztof Grobelny | b564594 | 2020-09-29 11:52:45 +0200 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <boost/asio/io_context.hpp> |
| 4 | #include <boost/asio/spawn.hpp> |
| 5 | #include <boost/asio/steady_timer.hpp> |
| 6 | |
| 7 | #include <chrono> |
| 8 | |
| 9 | namespace utils |
| 10 | { |
| 11 | |
| 12 | template <class F> |
| 13 | void makeDetachedTimer(boost::asio::io_context& ioc, |
| 14 | std::chrono::milliseconds delay, F&& fun) |
| 15 | { |
| 16 | auto timer = std::make_unique<boost::asio::steady_timer>(ioc); |
| 17 | timer->expires_after(delay); |
| 18 | timer->async_wait([timer = std::move(timer), |
| 19 | fun = std::move(fun)](boost::system::error_code ec) { |
| 20 | if (ec) |
| 21 | { |
| 22 | return; |
| 23 | } |
| 24 | fun(); |
| 25 | }); |
| 26 | } |
| 27 | |
| 28 | } // namespace utils |