r/cpp • • Sep 15 '26

Asynchronous API

https://grishavanika.github.io/async_api.html
36 Upvotes

28 comments sorted by

View all comments

Show parent comment

2

u/robertahleahy WG21, SCC, std::execution, Finance Sep 22 '26

You're supposing that everything in std::execution is core to the design. We don't apply that same standard to other things.

Particularly synchronous functions need extrinsic product and sum types to communicate heterogeneous return types. No one suggests that synchronous functions are excessively complex because of std::tuple and std::variant and std::visit and std::apply, those are understood to be outside the core design.

The same applies to std::execution. The core design consists of:

  • std::execution::sender and ::sender_in, which establish sender-ness, where a sender is a latent representation of an asynchronous function (i.e. it is not able to make forward progress but it is fully determined awaiting the selection of no further arguments)
  • std::execution::get_completion_signatures, which advertises the "return types" of asynchronous functions
  • std::execution::receiver which establishes that something represents a continuation (i.e. it is the asynchronous analogue of synchronous function return)
  • std::execution::connect, the process by which asynchronous operations (which can actually make forward progress) are formed (but not started)
  • std::execution::operation_state, the locus of an asynchronous operation, which is initially latent
  • std::execution::start, which begins asynchronous forward progress and binds both callee and caller to the so-called "receiver contract" (i.e. the caller agrees to keep the operation state within its lifetime until completion, and the callee agrees to report completion exactly once when the operation completes)

That's the complete design. You don't need std::execution::just or std::execution::when_all et cetera. Those are really nice to have, but you don't need them.

But let's take your objection more seriously/concretely. You want "a struct, a callback, that's about it." Fine. A struct:

template<std::execution::receiver Rcvr>
struct just_int_op {
  using operation_state_concept = std::execution::operation_state_tag;

  Rcvr rcvr_;
  int i_;
  
  constexpr explicit just_int_op(int i, Rcvr rcvr) noexcept : rcvr_(std::move(rcvr)), i_(i) {}

  constexpr void start() & noexcept {
    std::execution::set_value(std::move(rcvr_), std::move(i_));
  }
};

A callback:

struct just_int_receiver {
  using receiver_concept = std::execution::receiver_tag;

  constexpr void set_value(int i) && noexcept {
    std::cout << "Operation completed with " << i << std::endl;
  }
};

You can use these albeit non-generically:

just_int_op op(5, just_int_receiver{});
std::execution::start(op);

If you're willing to entertain one more struct you get the entire ecosystem:

struct just_int_sender {
  int i_;

  using sender_concept = std::execution::sender_tag;

  template<typename Sndr, typename... Env>
  static consteval std::execution::completion_signatures<std::execution::set_value_t(int)>> get_completion_signatures() {
    return {};
  }

  template<std::execution::receiver Rcvr>
  constexpr auto connect(Rcvr rcvr) noexcept {
    return just_int_op(i_, std::move(rcvr));
  }
};

Then you can start actually using this generically, but you don't have to:

std::execution::sender auto sndr =
  just_int_sender(12) |
  std::execution::then([](int i) noexcept {
    return i + 2;
  });
std::execution::operation_state auto op = std::execution::connect(sndr, just_int_receiver{});
std::execution::start(op);

Prints Operation completed with 14.