I’m trying to spawn a coroutine which would be watching the cancellation state, which itself would be emitted by a signal_set. However when I do try it, the exception get’s thrown once the thread gets deleted. So I guess something is left hanging?
co_await: The I/O operation has been aborted because of either a thread exit or an application request [system:995]
To be clear, I do not fully understand the coroutines and everything that goes with it, but I’m really confused with this one, as it’s really simple, yet I can’t make it work
#include <boost/asio.hpp>
static boost::asio::awaitable<void> foo() {
co_await boost::asio::this_coro::reset_cancellation_state(boost::asio::enable_total_cancellation());
auto cs = co_await boost::asio::this_coro::cancellation_state;
while (true) {
if (cs.cancelled() == boost::asio::cancellation_type::total)
co_return;
}
}
int main(int argc, char** argv) {
boost::asio::io_context ioc;
boost::asio::cancellation_signal signal;
boost::asio::co_spawn(ioc, foo(), boost::asio::bind_cancellation_slot(signal.slot(), boost::asio::detached));
boost::asio::signal_set sset(ioc, SIGINT, SIGTERM);
sset.async_wait([&signal](std::error_code, int) {
signal.emit(boost::asio::cancellation_type::total);
});
boost::asio::thread_pool tp(16);
for (int i = 0; i < 16; ++i)
boost::asio::post(tp, [&ioc]() { ioc.run(); });
tp.join();
}
What am I missing here? It looks like it should work.
gojko u. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.