by Anonymous » 15 Sep 2025, 00:00
Ich versuche, ASIO :: Co_Spawn mit Boost :: Future in der Thread -Bibliothek zu verwenden, sodass ich die CO_Routine -Aufgabe mit dann () ketten kann. Das
Problem ist jedoch ASIO :: Use_Future generiert nur std :: future . Zum Beispiel: < /p>
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;
}
Hier ist der variable Fut die Art von std :: future anstelle von boost :: future . class = "Lang-CPP PrintPrint-Override">
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;
}
Aber jetzt ist das Problem, dass wir zwei separate Threadpools haben, die auf ihrem individuellen Thread -Pool ausgeführt werden.>
Ich versuche, ASIO :: Co_Spawn mit Boost :: Future in der Thread -Bibliothek zu verwenden, sodass ich die CO_Routine -Aufgabe mit dann () ketten kann. Das [url=viewtopic.php?t=26065]Problem[/url] ist jedoch ASIO :: Use_Future generiert nur std :: future . Zum Beispiel: < /p>
[code]#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]
Hier ist der variable Fut die Art von std :: future anstelle von boost :: future . class = "Lang-CPP PrintPrint-Override">[code]#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;
}
[/code]
Aber jetzt ist das Problem, dass wir zwei separate Threadpools haben, die auf ihrem individuellen Thread -Pool ausgeführt werden.>