我有一个使用 cppcoro 写的库,希望在ASIO中使用它。但每当我尝试从该库
例如:
编译器报错,无法将两种类型合并在一起。
有没有办法从ASIO的io_context中调用基于
编译器:GCC 10.2 ASIO:1.18.1(不是Boost ASIO。这是独立版本)
co_spawn
一个协程时,Boost都会抱怨可等待类型不正确。例如:
#include <asio/io_context.hpp>
#include <asio/coroutine.hpp>
#include <asio/co_spawn.hpp>
#include <asio/detached.hpp>
#include <asio/awaitable.hpp>
#include <cppcoro/task.hpp>
cppcoro::task<int> foo()
{
co_return 1;
}
int main()
{
asio::io_context ctx;
asio::co_spawn(ctx, foo(), asio::detached);
ctx.run();
}
Boost 抱怨 cppcoro::task<int>
没有继承自 asio::awaitable
asio_cppcoro.cpp: In function ‘int main()’:
asio_cppcoro.cpp:15:43: error: no matching function for call to ‘co_spawn(asio::io_context&, cppcoro::task<int>, const asio::detached_t&)’
15 | asio::co_spawn(ctx, foo(), asio::detached);
| ^
In file included from /usr/include/asio/co_spawn.hpp:467,
from asio_cppcoro.cpp:3:
/usr/include/asio/impl/co_spawn.hpp:199:1: note: candidate: ‘template<class Executor, class T, class AwaitableExecutor, class CompletionToken> requires completion_token_for<CompletionToken, void()> auto asio::co_spawn(const Executor&, asio::awaitable<T, AwaitableExecutor>, CompletionToken&&, typename std::enable_if<((asio::is_executor<Executor>::value || asio::execution::is_executor<T>::value) && std::is_convertible<Executor, AwaitableExecutor>::value)>::type*)’
199 | co_spawn(const Executor& ex,
| ^~~~~~~~
/usr/include/asio/impl/co_spawn.hpp:199:1: note: template argument deduction/substitution failed:
asio_cppcoro.cpp:15:43: note: ‘cppcoro::task<int>’ is not derived from ‘asio::awaitable<T, AwaitableExecutor>’
15 | asio::co_spawn(ctx, foo(), asio::detached);
我也尝试将我的协程包装在asio::awaitable
中,但没有成功。
asio::awaitable<int> bar()
{
co_return co_await foo();
}
编译器报错,无法将两种类型合并在一起。
❯ c++ asio_cppcoro.cpp -o asio_cppcoro -std=c++20 -fcoroutines
asio_cppcoro.cpp: In function ‘asio::awaitable<int> bar()’:
asio_cppcoro.cpp:15:25: error: no matching function for call to ‘asio::detail::awaitable_frame<int, asio::execution::any_executor<asio::execution::context_as_t<asio::execution_context&>, asio::execution::detail::blocking::never_t<0>, asio::execution::prefer_only<asio::execution::detail::blocking::possibly_t<0> >, asio::execution::prefer_only<asio::execution::detail::outstanding_work::tracked_t<0> >, asio::execution::prefer_only<asio::execution::detail::outstanding_work::untracked_t<0> >, asio::execution::prefer_only<asio::execution::detail::relationship::fork_t<0> >, asio::execution::prefer_only<asio::execution::detail::relationship::continuation_t<0> > > >::await_transform(cppcoro::task<int>)’
15 | co_return co_await foo();
| ^
In file included from /usr/include/asio/awaitable.hpp:129,
from /usr/include/asio/co_spawn.hpp:22,
from asio_cppcoro.cpp:3:
/usr/include/asio/impl/awaitable.hpp:150:8: note: candidate: ‘template<class T> auto asio::detail::awaitable_frame_base<Executor>::await_transform(asio::awaitable<T, Executor>) const [with T = T; Executor = asio::execution::any_executor<asio::execution::context_as_t<asio::execution_context&>, asio::execution::detail::blocking::never_t<0>, asio::execution::prefer_only<asio::execution::detail::blocking::possibly_t<0> >, asio::execution::prefer_only<asio::execution::detail::outstanding_work::tracked_t<0> >, asio::execution::prefer_only<asio::execution::detail::outstanding_work::untracked_t<0> >, asio::execution::prefer_only<asio::execution::detail::relationship::fork_t<0> >, asio::execution::prefer_only<asio::execution::detail::relationship::continuation_t<0> > >]’
150 | auto await_transform(awaitable<T, Executor> a) const
| ^~~~~~~~~~~~~~~
/usr/include/asio/impl/awaitable.hpp:150:8: note: template argument deduction/substitution failed:
asio_cppcoro.cpp:15:25: note: ‘cppcoro::task<int>’ is not derived from ‘asio::awaitable<T, asio::execution::any_executor<asio::execution::context_as_t<asio::execution_context&>, asio::execution::detail::blocking::never_t<0>, asio::execution::prefer_only<asio::execution::detail::blocking::possibly_t<0> >, asio::execution::prefer_only<asio::execution::detail::outstanding_work::tracked_t<0> >, asio::execution::prefer_only<asio::execution::detail::outstanding_work::untracked_t<0> >, asio::execution::prefer_only<asio::execution::detail::relationship::fork_t<0> >, asio::execution::prefer_only<asio::execution::detail::relationship::continuation_t<0> > > >’
15 | co_return co_await foo();
|
有没有办法从ASIO的io_context中调用基于
cppcoro::task<>
的协程? 还有,为什么会这样呢?即使在C++ coroutines中是无栈的,所以对于任何协程实现而言,应该可以生成新的堆栈帧。那么为什么ASIO阻止我这样做呢?编译器:GCC 10.2 ASIO:1.18.1(不是Boost ASIO。这是独立版本)
cppcoro::single_consumer_event
所做的那样。但也许可以手动编写实现。 - Sergey Grigoryantsasio::use_awaitable
? - Exagonasio::async_result
的专门化即可。我在我的库TooManyCooks(厚颜无耻的自荐)中已经这样做了,链接在这里:github link,但是将其移植到任何其他C++20协程库应该不会太难——你主要需要修改aw_asio_base::callback::operator()
以将协程发送到正确的执行器以便恢复。 - undefined