How do I write a boost asio completion token which adapts to a non-asio coroutine library (PPL tasks)?

252 Views Asked by At

I'm trying to use Microsoft's Parallel Patterns Library's coroutines in my code, which uses Asio for most of its async stuff.

The cleanest way would be to define a token, use_ppl, which somehow behaves the same way as asio::use_awaitable but for ppl coroutines (there isn't a ton of documentation about pplawait.h it seems).

For example:

#include <chrono>
#include <iostream>

#include <pplawait.h>

#include "asio/high_resolution_timer.hpp"

#include "use_ppl.h" // doesn't exist yet

concurrency::task<void> print_after_wait(asio::io_context ctx) {
  using namespace std::chrono;

  asio::high_resolution_timer t(ctx);
  t.expires_after(2s);
  co_await t.async_wait(use_ppl);       // adapter here
  std::cout << "print\n";
}

I've checked asio\impl\use_awaitable.hpp for some pointers, but it seems like most of the code is very specific to asio's particular awaitable implementation (which makes sense).

I've also looked at cti::use_continuable_t from Naios's continuable library, which seems similar, but again it's pretty library-specific.

Does anyone have any pointers for how to go about this? What parts of use_awaitable are "boilerplate" and which parts do I need to actually specialize for PPL tasks?

0

There are 0 best solutions below