一起使用 cppcoro 和 ASIO 的 co_spawn

Using cppcoro and ASIO's co_spawn together

我有一个库是使用 cppcoro 编写的,希望将其与 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<> 的协程?另外,为什么会这样? AKAIF C++ 协程是无堆栈的,因此可以为任何协程实现生成一个新的堆栈框架。为什么 ASIO 阻止我这样做?

编译器:GCC 10.2 ASIO:1.18.1(不是 Boost ASIO。这是独立版本)

不幸的是,asio awaitables 和 cppcoro awaitables 不能相互co_await。发生这种情况是因为 asio awaitables 需要调用者和被调用者之间的合作来协调 shutdown/ownership。这就是为什么 asio awaitables 不满足 cppcoro Awaitable 概念的原因。 github 上有一个问题详细解释了这个问题:https://github.com/lewissbaker/cppcoro/issues/131

不过,您可以通过为 asio 提供协程包装器来将 asio 与 cppcoro 一起使用。为此,您可以使用 asio 的回调方法,并向处理程序提供 cppcoro::single_consumer_event,并在其中调用 event.set()

这里有一个例子:https://gist.github.com/isergeyam/ce10bef00abfaee3d0ec7f31a21c4b95