How to convert callback functions into Senders model?

125 Views Asked by At

For example, many functions in ASIO is callback-style (e.g. async_read_some()), and NVIDIA stdexec or Facebook libunifex is based on Senders model (std::execution).

How to make callback-style function and Senders model work together?

1

There are 1 best solutions below

0
On

Saying Asio async functions are "callback style" is probably not a fair summarization.

The "handler" is actually a Completion Token:

A key goal of Boost.Asio's asynchronous model is to support multiple composition mechanisms. This is achieved via a completion token, which the user passes to an asynchronous operation's initiating function to customise the API surface of the library. By convention, the completion token is the final argument to an asynchronous operation's initiating function.

It is true that the callback signature is kind of the "smallest common denominator" on which all the other behaviors are built. For this to work it is important to realize that handlers can be stateful (and move-only).

I'm not acquainted with the senders model. Given concrete code that I can run and test locally, I might be able to draw up a custom completion token that does the integration.

Mind you, when I see the actual form that code takes, the conclusion might be that it's easier to integrate things on the level of event loops. This is often the way to integrate in e.g. game engines or GUI frameworks.