![]() |
Home | Libraries | People | FAQ | More |
// // timer.cpp // ~~~~~~~~~ // // Copyright (c) 2003-2026 Christopher M. Kohlhoff (chris at kohlhoff dot com) // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // #include <iostream> #include <boost/asio.hpp> boost::asio::awaitable<void> print() { boost::asio::steady_timer t(co_await boost::asio::this_coro::executor, boost::asio::chrono::seconds(1)); int count = 0; co_await t.async_wait(); while (count < 5) { std::cout << count << std::endl; ++count; t.expires_at(t.expiry() + boost::asio::chrono::seconds(1)); co_await t.async_wait(); } std::cout << "Final count is " << count << std::endl; } int main() { boost::asio::io_context io; boost::asio::co_spawn(io, print(), boost::asio::detached); io.run(); return 0; }
Return to Timer.7 - Using a coroutine to implement a repeating timer