Reputation: 41
A simple function:
awaitable<std::size_t> write(const std::vector<uint8_t>& data)
{
auto token = co_await this_coro::token();
return co_await async_write(serialport_, buffer(data), token);
}
Can be awaited using co_await write(my_data)
.
This works when I use any async Boost ASIO function.
How would one go about using this pattern if the async function was some callback from some unrelated library?
The code would potentially look something like:
awaitable<bar> foo()
{
auto token = co_await this_coro::token();
return co_await third_party_callback;
}
And usage would be something like auto result = co_await foo()
.
I am having difficulties figuring out what the simplest/cleanest way to this is.
Bonus question: What is the relationship between Boost ASIO's coroutines API (e.g. the token), Boost's coroutines library, and Coroutines TS?
Upvotes: 4
Views: 3295
Reputation: 14138
To use another library, the other library would have to create support for coroutines TS or you (or someone else) would have to provide the "glue" code between the what the coroutines TS requires to work and the 3rd party library.
The effort to do this may not much once you understand what the coroutines TS requires to work.
You could read Lewis Baker articles on the co routines TS. There are lots of videos and articles from lots of people covering the topic now. Once you understand the requirements, supporting other await types is simple so long as you have some way to signal when the co routine completes and you also have some sort of context to continue the co routine completes on.
If the third party library is just do some heavy processing work. You may like to wrap the work into some sort of boost future / promise setup and use the boost thread glue code that is already around like this one (although the boost asio examples predate the boost asio experimental support.
The experimental token is the "glue" code between the co routine TS and boost asio (as far as I can tell). It has nothing to do with Boost's coroutine library.
Boost ASIO supports 3 co routine types:
Upvotes: 4