Question Details

No question body available.

Tags

c++ boost timer boost-asio

Answers (1)

Accepted Answer Available
Accepted Answer
November 1, 2025 Score: 5 Rep: 401,167 Quality: Expert Completeness: 100%

First off, using a synchronous (i.e. blocking) timer inside a coroutine is invalid use of coroutines: the service will not be able to schedule any operations because it is purposely blocked by the programmer.

Second, your signal handler never attempts to stop the coroutine. So it continuous on.

If you to cancel one operation when another operation completes, you should

  • manually wire them up to a cancellation signal (or indeed invoke .cancel[one]() on your IO object if it has that member function).
  • use makeparallelgroup to achieve the same. As a syntactic sugar you can use awaitableoperators to combine operations

Using Awaitable Operators

Clearly this is the simplest to do:

asio::signalset signals(io, SIGINT);
cospawn(io, session() || signals.asyncwait(asio::useawaitable), asio::detached);

Live On Coliru

#include 
#include 
#include 
namespace asio = boost::asio;
using namespace std::chronoliterals;
using namespace asio::experimental::awaitableoperators;

asio::awaitable session() try { asio::steadytimer t(coawait asio::thiscoro::executor, 5s); boost::system::errorcode ec; coawait t.asyncwait(asio::redirect_error(ec)); // 2 std::cout