gpt4 book ai didi

c++ - 一起使用 cppcoro 和 ASIO 的 co_spawn

转载 作者:行者123 更新时间:2023-12-04 11:35:25 27 4
gpt4 key购买 nike

我有一个库是使用 cppcoro 编写的并希望与 ASIO 一起使用。但每当我尝试 co_spawn来自所述库的协程。 Boost 提示 awaitable 类型不正确。
例如:

#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();
|
有没有办法拨打 cppcoro::task<>来自 ASIO 的 io_context 的基于协程?另外,为什么会发生这种情况? AKAIF C++ 协程是无堆栈的,因此应该可以为任何协程实现生成新的堆栈帧。为什么ASIO阻止我这样做?
编译器:GCC 10.2
ASIO:1.18.1(不是 Boost ASIO。这是独立版本)

最佳答案

不幸的是,asio awaitables 和 cppcoro awaitables 不能相互 co_await'ed。发生这种情况是因为 asio 等待需要调用者和被调用者之间的合作来协调关闭/所有权。这就是为什么 asio awaitables 不满足 cppcoro Awaitable概念。
github上有个issue详细解释了这个问题:https://github.com/lewissbaker/cppcoro/issues/131
不过,您可以通过为 asio 提供协程包装器来将 asio 与 cppcoro 一起使用。为此,您可以使用 asio 的回调方法,并提供 cppcoro::single_consumer_event到处理程序,然后调用 event.set()在那里。
这是一个例子:https://gist.github.com/isergeyam/ce10bef00abfaee3d0ec7f31a21c4b95

关于c++ - 一起使用 cppcoro 和 ASIO 的 co_spawn,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67352712/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com