Code: Select all
#define BOOST_THREAD_PROVIDES_FUTURE 1
#define BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION 1
#define BOOST_THREAD_PROVIDES_FUTURE_WHEN_ALL_WHEN_ANY 1
#include
#include
#include
#include
#include
using namespace boost::asio::experimental::awaitable_operators;
namespace asio = boost::asio;
auto running_task() -> boost::asio::awaitable
{
std::println("starting the coroutine ....");
auto timer = asio::system_timer{ co_await asio::this_coro::executor };
timer.expires_after(std::chrono::seconds(10));
co_await timer.async_wait(asio::use_awaitable);
std::println("finishing the coroutine ....");
}
auto main() -> int
{
auto io_context = boost::asio::thread_pool{ 4 };
auto fut = asio::co_spawn(io_context, running_task() , asio::use_future);
io_context.join();
return 0;
}
Code: Select all
#define BOOST_THREAD_PROVIDES_EXECUTORS 1
#define BOOST_THREAD_USES_MOVE 1
auto main() -> int
{
auto io_context = boost::asio::thread_pool{ 1 };
auto thread_pool = boost::executors::basic_thread_pool{ 1 };
auto fut = boost::async(
thread_pool,
[&io_context]()
{ return asio::co_spawn(io_context, running_task(), asio::use_future).get(); });
auto fut2 =
fut.then(thread_pool,
[&io_context, &timer](auto fut)
{
fut.get();
return asio::co_spawn(io_context, running_task() || cancel_routine(timer), asio::use_future).get();
}
);
fut2.wait();
io_context.join();
return 0;
}