Skip to content
PodSubs
ShowsPeopleGuestsJobsNewsEvents
More
BlogPodCred
Menu
ShowsPeopleGuestsJobsNewsEventsBlogPodCredNetworksSign in
Join nowSign in
  • Shows
  • People
  • Jobs
  • News
  • More
    • Guests
    • Events
    • Blog
    • PodCred
    • Networks
    Join nowSign in

ADSP: Algorithms + Data Structures = Programs

ADSP: Algorithms + Data Structures = Programs artwork

Episode 231: C++26 Senders and Receivers (+ flat_map)

Apr 25, 202544 minAudio
ShareXLinkedInFacebookWhatsAppEmail

In this episode, Conor and Ben chat about C++26 Senders and Receivers, flat_map and more. Link to Episode 231 on Website Discuss this episode, leave a comment, or ask a question (on GitHub) Socials ADSP: The Podcast: Twitter Conor Hoekstra: Twitter | BlueSky | Mastodon Ben Deane: Twitter | BlueSky Show Notes Date Generated: 2025-04-09 Date Released: 2025-04-25 ArrayCast Episode 103: Julia - an Array Language P2300R10 - std::execution C++26 Senders and Receivers C++ std::optional::and_then Haskell join The Mother of all Monads Chains: Exploration of an alternative to Sender/Receiver | Sean Parent | NYC++ Intro Song Info Miss You by Sarah Jansen https://soundcloud.com/sarahjansenmusic Creative Commons - Attribution 3.0 Unported - CC BY 3.0 Free Download / Stream: http://bit.ly/l-miss-you Music promoted by Audio Library https://youtu.be/iYYxnasvfx8

Audio and video play straight from the show's own hosting. PodSubs stores and serves no media.

Transcript

From the show. Tap a time to play from there.

BenSo the important parts, the three important things about sends and receivers. One, independent of mechanism of concurrency, it runs with thread pools, it runs with coroutines, it runs with interrupts, right? So that's one of the things that makes it very amenable to application either in the big data space or in the tiny embedded space with interrupts. Very important. Second important thing, it is asynchronous function composition, right? If you think about it like that, that's another very easy way, a good way in to think about it.

BenNothing happens when you declare senders. You can declare these things all day, you can compose them all day. No work happens at that point. It only happens when you say, kick off the work. And the third important thing is the uh what's called structured concurrency, which is to do with lifetimes.

ConorWelcome to ADSP the podcast, episode 231, recorded on April 9th, 2025. My name is Connor, and today with my co-host Ben, we chat about senders and receivers, flat map and more.

BenMy other topic was talking about uh senders and receivers, you know, because you and you and Bryce often talk about parallelism concurrency, and so I thought we it might be interesting to talk about senders which are you know in C26. And I wanted to uh Bryce made a claim that I want to rebut. Uh or finesse, maybe. So Bryce made a claim in the previous podcast that um I have a quote here.

BenToday we live in a parallel world. Almost every hardware platform is parallel from the smallest embedded devices to the largest supercomputers. I would finesse that by saying that embedded devices have concurrency but not much parallelism. And therefore that almost all hardware platforms in the world today have no parallelism because almost all hardware platforms are embedded devices. Um but it is certainly true to say they have concurrency, mostly in the form of interrupts. And so, you know, I wanted to, I thought maybe senders would be an interesting interesting thing to talk about.

BenUh, you know, in particular to talk about um the two worlds that it addresses, you know, Bryce and you live and you work for NVIDIA, you live in the world of huge hardware, right? Massive computation. I live in a completely different world, which is tiny hardware, no computation, but still concurrency. Still the problem of concurrency in the form of interrupts. And senders, I think, addresses both worlds. For for a long time it was developed by folks firmly with their feet in the big hardware world.

BenBut, you know, uh the work I'm doing with my team with Michael and with Luke, um, we are we are applying it to the very, very small, to the embedded, to the world of interrupt, not just to the world of huge, possibly heterogeneous hardware, large computation. Um, so it's really interesting for me to sort of contrast those two ideas and and yet recognize that I think senders has a good has a good answer to both. I don't know. What do you think?

ConorWell, so I think we should we should start off this topic. It's a huge topic, I swear to having you give an overview of what senders and receivers, we've talked about it before, and admittedly, I said the other day when I was recording uh an array cast podcast, we were talking about Julia and the topic of multiple dispatch came up. And I I said to the guest, you know, multiple dispatch is one of these things which I have a surface level understanding of, but I've never really like it's uh the Julia folks talk about how it's it's the killer feature of the language.

ConorAnd I'm like, I don't I clearly don't understand it comprehensively enough to understand uh what the big deal is. And then even after he explained it, I think it my understanding improved, but it still wasn't like uh I 100% could go give a lightning talk on this. And I feel the same way about senders and receivers. The my background on it, I've uh perused, I can't remember which version of the P2300 paper. I I believe that was the the last one. I think we're up to at least uh 10. Okay. At one point I I read most of minus the you know the code the code snippets um that paper.

ConorAnd the most helpful thing that I had was because Eric Niebler works at uh NVIDIA, at one point I I grabbed uh an hour of his time and he actually walked me through, admittedly, he was driving, I wasn't doing any typing, uh, a bare bones implementation. And and the the thing that I I've heard and I think understand is, you know, in in Eric's words, what iterators are for serial algorithms, uh like in you know, C 11, is what senders and receivers are for asynchronous algorithms.

ConorAnd I I understood, you know, uh Eric's little presentation, and I understand at a high level, but I definitely don't I'm in the same position with you know Julia's multiple dispatch. I don't feel like I could go give a five-minute lightning talk and be like, you know, here's the explain the what is it, Eli Five Explain like I'm five?

BenI think I can I can probably give a good account of that in in five or so minutes. I c I I feel like I'm up to that.

ConorAnd we should mention, I think it's been mentioned on the podcast before, but you yourself are in a position to do that because you have implemented basically a version of it yourself, which is the number one best way, in my opinion, to understand something comprehensively is just to implement your own version.

BenI have uh it's not an it's not a completely standard version, but it is certainly I have implemented senders and receivers in in a in a code base which will be in production, and certainly to the same platonic ideals as exist in the standard, and to mostly the same interfaces, some of the details differ. Awesome.

ConorSo over to you, and uh help me and our listeners, because I guarantee you that more than 50%, maybe more than 75 or 90, are in the same boat as me.

BenUm, senders and receivers, firstly, it it's a framework for expressing asynchronous computation. Um and the name's a bit of a misnomer, so um or or doesn't really give the idea of what it is, let's say. Really, senders, forget about receivers for a minute. Senders are asynchronous functions, right? So when you call a regular function, you you give it arguments, you get a return value. When you call a sender, you give it arguments, you get a return value, but you don't get that return value through a return statement.

BenInstead, that return value we might say goes on to the next sender as a continuation, right? So because this is asynchronous functions, we're not we're not exactly executing it like there are some similarities, we're not really executing it like a regular function stack and returning values. Instead, we're passing the values, the what would be the return values, onto the next continuation, which is the next sender in the chain. So at a very high level, that's what it is.

BenSo some things we can note. Senders and receivers is asynchronous computation embodied, and it does not therefore tie itself to any particular method of concurrency, right? So when you're writing an asynchronous system, there are there are three things which which you want to separate. One is the expression of the functionality, the business logic, if you like. That's what senders is concerned with.

BenBut the other two things are firstly the mechanism of concurrency, if you're going to use thread pools or interrupts or or you know, whatever, or deferred queue, or whatever, there are many mechanisms we can use to achieve concurrency. Senders don't care, or coroutines, right? Senders don't care, you can use any of them. And the other thing is like what's the strategy of parallelism, right? Which things do we actually want to occur at the same time properly parallel? Um, and those three things, the business logic, the mechanism of concurrency, and the strategy of parallelism, we can separate out.

BenUh and and and senders is concerned with expressing the asynchronous computation in a way that does not concern itself with the mechanism of concurrency. A sender is an asynchronous function, right? When you make a sender, it's just a no work happens, right? You you declare a sender and you give it, you know, like some like you might give it a a a lambda to do. Um like the first the simplest sender is called just.

BenAnd you give it you give it a value, and when you run that sender, it will produce that value. In other words, uh it will send that value downstream to the con next continuation, the next sender, yeah rather than returning it like a function would.

ConorSo the senders are um when you compose these, you get a basically representation of a computation, but nothing happens.

BenAnd then the strategy for parallelism and the strategy for parallelism comes in in the ways that you compose senders together, or sometimes in the algorithms that you use to compose senders together. But the mechanism of concurrency, whether you use thread pools or coroutines or whatever, that's completely independent. So having having having declared our just sender, let's say we say, you know, just 42. So when we run it, it's gonna just send 42. We can run that sender on the senders and receivers framework abstracts this to the idea of a scheduler.

BenSo a scheduler embodies the idea of um where I'm gonna run this. that does coroutines or whatever. A scheduler, you can ask it for a sender. And the sender that corresponds to a scheduler represents the entry point into the computation. Uh it does nothing, but it just represents starting the computation on whatever computation resource that scheduler represents.

BenSo the very simplest one actually is just the scheduler that doesn't uh a function, a regular function call, right? That's a perfectly valid scheduler. And it's called, I think, the inline scheduler, not to be confused with like inlining functions, but executes the computation inline in the current thread, just as if you had called a regular function, right? That's a perfectly valid scheduler. And um, when you ask it for a sender, it will give you back a sort of regular function. But then you can compose more senders with that, so you can say then, then is a sender algorithm that is that is fmap.

ConorRight. Similar to the and then on the optional API then. Yeah, exactly. Cybran got got into the standard and for and friends.

BenYeah. So it receives what the sender before it sends, it it transforms it, and it sends on the result of the transform. It's like it's like transform on vector, right? It's like and then on optional. It's working on the value, in this case, inside the sender chain. So this is senders implement the continuation monad. Just like vector implements the list monad.

BenThe list monad, yeah, the list monad. Uh and optional implements the the maybe monad, right? Right. In the same way, senders implement the continuation monad. So you have the same kind of monadic uh ideas, they are not in C expressed in functional style, but they are given names that are chromulant with C, right? Right. So we have um just is like lifting a value into the monad, right?

BenIt's what Haskell calls pure or return. Uh and when you say just 42, then you get 42 in your continuation, right? Being sent downstream. Then is like transform, we said let value is a third algorithm that is like then, except it doesn't return, it doesn't transform the value and return a new value, it returns a new sender, right? So it's like um you can make a choice of which sender to run next based on a runtime value that came to you down the continuation.

BenIt's monadic bind, if you're familiar with that. And the purpose of monadic bind is to make that runtime choice when you have that data dependency. When what you run next depends on what was sent to you.

ConorWhat is a monadic bind in the like RXJS reactive world?

BenYou mentioned concat earlier, it's mapconcat. It's transform reduce.

ConorMapconcat.

BenWell, it so imagine your transform produces a vector from each element of your vector, and then you concatenate them all together in your reduction. Uh some languages call it flat map, some call it mapconcat.

ConorOkay, yeah. I mean I definitely am familiar with flat map, but in my mind that's like you're taking a nested structure and then this is when you need uh Hoogle Translate.

BenLet's look at the optional version. We mentioned and then. Okay, so it's and then on optional, actually. Because then is called transform on optional. And and monadic bind or let value is called and then on optional.

ConorOh no. So then is called transform and and then.

BenBecause the function you pass to and then itself returns an optional. That's the key. So like you have a you have a pipeline with optional where each step of the pipeline returns an optional result. Right. Like you go fetch some HTML content, right? The you make a connection, the connection might fail. You you fetch the URL, that might fail. Each step is basically a function from a sort of concrete thing, thing not s the thing that's not optional, to an optional thing.

BenRight? And the way you chain them together is and then.

ConorRight. So I mean in a very simple case of this optional example, you know, transform takes a unary operation. Say we've got a uh uh optional of an int, it's got some value, and we want a plus one. You just send the lambda that you pass to the transform, its signature is int to int. Because it's gonna do the unboxing of the optional if it's there, and then calculate the value and put it back.

ConorWhereas and then its signature is int to optional? The function you pass, yeah. Imagine if you wanted to do or the function that you pass to and then, yes, yes, sorry.

BenImagine if you wanted to to transform with taking a square root. Right? What are you gonna do if there's a negative number inside your optional? Yeah, yeah, yeah. Your square root not not the standard square root, but you might choose to make square root return an optional, right? Because it can fail. Right. It might be undefined. Um and so if you transform with that function, the result you get would be an optional optional int, right? Because you've taken the int inside the optional and you've transformed it to something that now produces an optional int.

BenSo you've got a doubly wrapped int in the optional. Right, right, right, right. So what you do instead of transform is and then. Right? Which effectively collapses down those two optionals so that you stay in just a singly wrapped optional.

ConorInteresting. This is this is well, I don't know.

BenThis is what monadic bind is.

ConorYeah, this is um I've always been a little bit confused by flat map, but I'm not sure if this is a Eureka moment, but definitely I think my understanding of something is is clarifying here because there is a function uh or this flat map, and you uh what I am about to say could be wrong, and Ben will correct me if so. Uh hopefully. Okay. That like you can think of if you want to go from we'll go to ArrayLand, uh, where you know you've got a matrix representable as a list of lists in any language like Python or a vector of vectors in C.

ConorAnd sometimes you want to turn that into a flat list and then maybe map once again, plus one on that that uh matrix. So you want to go from matrix to vector rank one array and then do an operation. And definitely in certain APIs, there you can do this with something like a flat map, where they similar to the way that you can do a uh transform reduce in one call in C, you could also do that with a transform followed by another call to reduction.

ConorAnd I know like in Rust and other APIs, they have the map, they have the fold, but they also have algorithms called fold map or map fold or whatever. And so is this the case where so I uh my guess actually is where flat map in in actually is not going from like 2D to 1D. It's you're in the list monad, aka a rank one vector, a list. You're doing some operation that could explode it to a list of lists, and flat map keeps it as like sure you you go to a a mate, so instead of ending up with a list of lists, you're gonna end up still with just a list uh because everything has been flattened, which uh the Eureka moment that I was talking about is that like I'm conceptualized of going from like 2D down to 1D, but really it's 1D within your operation you could go up, which is similar to the option of an option if you if you have some failures.

ConorSo you need to make sure that you've only got one uh level of nesting basically, whether that's the list monad or the maybe monad. And some I think that there's a bit of conflating. I'm sure there is an operation similar to fold map that is the flat map that actually goes from 2D to 1D. But in this case, the flat map that we're talking about, the monadic bind we're talking about, it just you're at one level of nesting and you want to avoid, in certain cases, going up to two, whether that's a list of lists or an optional of optionals. Does that sound about right?

ConorAnd also maybe flat map in no language actually does it does this 2D to 1D. Um maybe that maybe maybe I've always just misunderstood flat map and it never does that.

BenWell, so there are a couple of ways to get there, right? You can either I mean you can maybe fuse elements of it in array languages and and also you can you can generate the lists of lists and then concatenate them, or you can um sort of go the other side. I think I'm thinking of it as like the other side of the square where you I guess in the case of in the case of bind it's effectively the same thing. But it's it's it's yeah, it's it's uh map followed by join, if you like.

BenJo join is join is the other monadic term that gets used here. Um in Haskell there's a function join. Yes. Um and and map followed by join is bind.

ConorOkay, well, so let's let's have a little tangent fun.

BenIt's uh Yeah, we've come off of send and receiver, now we're talking about I I've so I've got another example, um another maybe good example which might help.

ConorBut before we do that though, let's let's have this tangent fun, which is something that I've I've never really taken time to think about. And uh I apologize to the listener if we're about to lose you, but this is the kind of stuff that uh uh I know Ben spends time thinking about, and uh I should have. So join in Haskell corresponds to the W combinator, W. Which is it sounds highfalutin, what I just said, but it's actually quite simple. It takes a binary in in the space of functions, which I actually don't know.

ConorWhat is it? Is there a function monad? I don't actually know what is that the function monad of function composition, it's yeah is a type of monad. And so in array languages, it's super, super simple. It takes a binary operation and it turns that into a unary operation by duplicating the argument. And that's actually in in most array languages it it is called duplicate, even though it has a glyph or a symbol. Sometimes it's called um self, and I think there's one other name for it. But the idea being that, like, you know, if you want to square something, that is just the W combinator applied to multiplication.

BenSo now think of your binary operation as a nested a unit operation nested inside another unary operation, because in Lambda calculus that's how it might be expressed, right? You you would bind one argument, getting back a partially applied function, and then you would bind the second argument, and that is a binary operation.

ConorHere I am thinking this is gonna be like a seven-minute tangent, and then basically it took you ten seconds and like three seconds into your explanation, I was like, oh wait, that's just but that's a weird I mean it so that So it's a function inside the function the same way as an optional inside an optional or a vector inside you know vector of vectors. That is amazing. I hope I hope that was I mean it I got three seconds into your explanation, it clicked. And I've never I've never understood how I guess because I've never taken time to think about it because it's actually pretty straightforward that the equivalent of copying your argument to a binary function is Just really nesting.

ConorWell, actually no, now that I say that. I mean it's it's not invoking the binary function, it's just like a what you would call it like a partial application. Yeah, yeah. And I guess in Haskell that works because everything is technically like a unary function.

BenUm partial application is built in.

ConorCould you build I mean I guess we we have we have this on the optional interface, but like how would you do this for functions? Would you would in C ⁇ you'd do this with like bind first just to call the bind first twice?

BenYeah, I guess so. In C ⁇ we don't have very good tools for function composition, but you could build similar things. Bind first might might be part of it. We don't we don't tend to express, you know, for historical and maybe for efficiency reasons, we don't express partial application very much. It's fairly niche thing. But it is mathematically working out to the same thing as as as energy functions.

ConorYeah, I I wonder what the what the disconnect of like trying to spell this, because like you or it does it is does this rely on in a functional language like Haskell, does it rely on currying? Like how are you able to apply this like nest yourself within yourself and then you end up with a unary function? Like there must be a way to do this in C with a function, like if you take std colon colon colon plus brace brace, like the function object. I mean you you definitely can write a lambda called w that like does the copying, but is there Yeah, yeah.

BenAnd you can write a curry. You can write a curry function, a higher order function in C. And a while ago, this was a topic of discussion at my meetup where we we sort of worked it out, and I think Jason put it in one of his talks.

ConorYes, I recall seeing this a while ago a while back. Anyways, all right, I'll think about the C side of this, but definitely definitely that well makes 98% sense minus the C P.

BenSo we strayed a little bit from senders. Uh actually the the other example the other intuition I was gonna give for for join or mapkin cat um was uh imagine writing a chess program, right? So imagine a very simple uh kind of I mean chess programs these days are much more involved, obviously. But imagine you're in 1985, writing a naive chess program. Um you'll fundament fundamentally what what you're going to do is look at the position, generate all the moves that could be made, right?

BenFrom those moves, from those new board positions, again generate more pos more more moves. You know, you're generating a tree of board positions based on moves, and you're and then you're scoring them, obviously. But just considering the generation, um you're sort of you're doing each, you know, you start with your one board position, the current one, and you produce n new board positions based on the moves that can be made. Each one of those n goes on to produce m more, right, based on moves that can be made from that position.

BenYou want to collapse all these down, you know, in the process of scoring them, you're you're also um selecting the new the new best board position, right? And so it's that kind of operation where you are generating each each board position can generate n. So that's your function from value to like list of values, right? You're in the list monad here.

ConorYes, that makes complete sense. And and to clarify, from from my understanding, is that you're reducing this list, the flattening happens at every step. You're not you're not doing like the what they call it, um what did Alpha go? It was the uh MCTS, Monte Carlo tree search. That one goes out n steps, yeah, and then after a certain number of steps, it chooses the best path and then repeats. Uh whereas this one, your tree search is is well you want to flatten it down, yeah.

BenI think one of Bartosz's talks that he gave at C now maybe ten years ago, had this. He talked about the n queens problem, which was which is very similar. Right, right. You know. So you're trying to you you put a queen down, and then that constrains the space. There are only so many spaces you can put another queen down, and but there is more than one place, right? And so you are searching this tree of of boards, of valid positions, and he uses a monadic approach to do that.

ConorInteresting. Well I'll see if I can find that talk. Uh I definitely know I haven't seen all of Bartosz's C now talks. I've seen a couple, but I don't recall seeing that one. So I mean to bring it back to senders and receivers, how does this we're now we now go to the continuation?

BenWhich I feel now I have not given the five-minute explanation of it.

ConorI was gonna say you mentioned continuation monad after the first five minutes. So I think probably the first five minutes you were good.

BenThen you said that, and then I was like, uh So the important parts, the three important things about senders and receivers. One, independent of mechanism of concurrency, it runs with thread pools, it runs with coroutines, it runs with interrupts, right? So that's one of the things that makes it very amenable to application, either in the big data space or in the tiny embedded space with interrupts. Very important. Second important thing, it is asynchronous function composition.

BenRight. Right? If you think about it like that, that's another very easy way, a good way in to think about it. Nothing happens when you declare senders. You can declare these things all day, you can compose them all day. No work happens at that point. It only happens when you say, kick off the work, wait for the wait for the calculation to finish. Right. Right? So senders are descriptions of work, they aren't kicking off the work. The work kicks off when you say something like sync weight, which is an algorithm that consumes a sender, or remembering that a sender is a composition of senders, potentially, right?

BenThese things that are infinitely composable. So when we say a sender, we might mean a single, like just, or we might mean a whole chain of just produce, then do this, then something else, then right. Senders are composable. And the third important thing is the uh what's called structured concurrency, which is to do with lifetimes, right? So when you're in regular function call land, synchronous function call land, um you have a stack.

BenAnd the lifetimes of your variables are implicit because of that function call stack, right? So when you call a function, everything the parameters you pass you pass in like references to variables on the stack, those variables stay alive during that function call because it's only when the function returns that the current stack is popped. And you know, the stack ensures that lifetime, the lifetimes of variable uh arguments cover the lifetimes of the functions you call them with.

BenAnd there's a similar thing happening with senders, except that it's not a stack. Um, these things are called operation states. So when you connect a sender to a receiver, the resulting thing is the operation state. I'll talk about receivers in a sec. But the operation state is that lifetime object. That's where the state lives for the computation. That's the object whose lifetime covers the computation. Right? And because senders are composed together, in a very similar way, operation states get composed together and they form this kind of what's been described as an onion of states that that the outer states wrap the inner states, right?

BenAnd so the lifetimes of the variables persist, and it's uh you know, the the lifetime of the variables in the outer skin and then in the skin inside that and the skin inside that all the way down to the center of the onion. Um similar to a function called stack where you can think about variables living at levels of the stack, you can think about variables living at on layers of the onion. Okay, I think that makes sense, yeah. And the the the first sender that you run is the innermost part of the onion, right?

BenWhen that completes, the next sender in the chain is the next level of onion skin, and it it it the computation proceeds all the way out to the outer layer. When the final sender completes, the lifetime of the whole object is over. So that's how when people talk about structured concurrency, that's what they mean. It's this this framework takes care of lifetimes in a in a way that can be reasoned about, similar to the way we reason about lifetimes on the stack when we call functions.

ConorRight. It is interesting, you know, in this conversation, you know, for better or for worse, unfortunately, I've I've probably uh steered this in a less digestible thing because I got excited about uh understanding join. But there is this like I don't know if it's isomorphism is the correct word, but when you think about you know the list monad versus the we'll call it function monad, even though that's might not be what it's called, and then the continuation monad, which we're talking about, and they call each of these operations different things, there really is like a one-to-one like equivalence almost.

ConorLike you can you can think of you know, uh senders and receivers give you the ability to represent an asynchronous graph of like computation. And you can think of it in a way like if you take uh a bunch of functions that you know take parentheses and multiple arguments, and you nestedly like Russian doll call these in a different order, that's a way of representing like a graph of computation, right? Like you have to, you know, work your way down to the most inner functions, evaluate those, pass it back up.

ConorThe same way with combinatory logic and combinators, you can chain you know operations together, which is also going to represent like this. And my guess is that this senders and receivers, because it's the continuation, you could actually use this to build versions of all these different, like you s you said, because it's separating the not that that's the way to teach this or explain this, which is why I said for better or for worse, is the kind of the the where the but we can revisit this topic and um it's true.

BenThe continuation monad is very powerful and can express other monads. In fact, there's a blog post out there where someone calls it, I think, the mother of all monads. Yeah. We will I'll find that blog, link that blog, and uh we can also observe, of course, that um asynchronous computation is a superset of synchronous computation. In other words, we can express synchronous computation in an asynchronous way. We can't do the other way around, but you can always collapse an asynchronous computation into asynchronous one by running it on the aforementioned inline scheduler that just does function calls.

BenUm and so this there's a lot of machinery in senders, but it can all boil down to exactly it if you actually want to run it synchronously, it boils down to nothing less efficient than function calls. Right. And like everything in C, if you run it asynchronously, the idea is that it runs exactly as efficiently as if you had hand coded everything.

ConorYeah.

BenAt least as efficiently.

ConorIt makes me think of uh some of the things that I look at uh for work are Jax, which is uh a library out of Google, and Kupy, which is a uh GPU accelerated like version of um NumPy. And both of these have decorators in Python that you can use to um basically jit compile certain functions. And internally they will once again create a graph of computation based on the function calls, and then they see what they can optimize. And so you're building a graph, but in the simplest case, you can just have a function that linearly calls one thing and then another thing and then another thing, and then your graph is just a linked list, right?

ConorWhich is just serial computation. Like I mean, technically a linked list uh is a graph, it's just a very simple one, right?

BenUm Yeah, yeah. And if you run it on an inline scheduler, it more or less literally becomes a regular function call stack.

ConorYeah.

BenSo the other thing we can we can so I haven't mentioned receivers yet. Receivers, the the proposal is often called senders and receivers. The important thing really is the senders. They're what express the computation. The receivers mostly don't occur in user code, they're mostly used for the glue between the senders that algorithms use to plug these things together. One of the important things that senders gives us is not just the happy path called the value channel, right? If everything goes right, you just you're on the happy path. You're in the value channel, everything's succeeding.

BenBut also there are two other channels. The error channel, right? When something goes wrong, you you want to be able to handle an error, you want to be able to potentially short circuit the computation, error out, and handle it a different way. And the cancellation channel or the stopped channel, right? You want to be able to tell from the outside this thing to stop because uh something else changed in the world and you're no longer interested in waiting for the result, right? Events have overcome it. And these are really important, and these are also built into the sender's framework.

BenSo every sender can complete in those three different ways, with a value, with an error, or by having stopped, and every receiver handles those three channels. And so this is built into the idea of the framework of asynchronous with senders, uh, the capability of cancelling the work or handling an error, as well as just staying on the happy path. Now, because senders are completely agnostic about the mechanism of concurrency, the cancellation has to be cooperative.

BenThere is no stop the thread or anything like that because senders don't know anything about threads or coroutines or whatever else you're using for your mechanism of concurrency. So you don't get to preempt them or anything like that. What you get to do is request that they stop, right? And and they have to take heed of that request to be good citizens, which you know they will do because at the end of the day, you will be writing the the business logic.

BenRight.

ConorIs this one of I mean uh my memory is failing me now. I remember when Bryce and I were talking to Sean Parent, he gave a talk at I want to say, yeah, it wasn't Adobe office in New York for a meetup, and he presented I think it since then he's probably presented at a yeah at a conference. He Chains, and he was pointing out a potential flaw in the or flaw, quote unquote, you know, uh a trade-off uh in the design of senders and receivers, and that was what Chains was addressing. Was this was this it or was that something else?

BenUm because I chains I did see Sean's talk at C now, but I don't remember all the details. But yeah. I think change is basically an alternative way of expressing asynchronous function computation. It's not so very different from sends and receivers, but it's a little bit different in the details. And uh yeah, it's a it's a trade-off, I think. It's a it's a different, slightly different design. Um I think in particular, one of the things perhaps that and I and I'm sort of talking just based on remembering now, there is there there's a design choice to be made about external control of lifetime.

BenLike if I delete the object, am I allowed to delete the object? Should that result in a request for it to stop? Should that result in unwinding the chain of of computation? There are different choices that can be made there. And I think chains makes a different choice than senders in that regard.

ConorYeah. Yeah, I'll have to go back and re-watch that. Or I mean when we bring Sean back on, because we already mentioned him two episodes ago, we can we can just ask him directly. So yeah, I mean I I definitely I mean I still feel I feel like I have a better understanding, but like when you start talking about the um onion analogy of uh you know wrapping uh the state operation state, you know, I it my head immediately goes to like how would I implement this? And I I I feel like I I would still it would take me a while to go and implement I don't know, every once in a while it's like I have this a better surface level understanding, but still could I go and implement this thing like right now, snap of the fingers.

ConorAnd I probably could, but I would I would struggle. Whereas, you know, something like a combinator model, I can go implement that with my eyes closed because I sure sure and uh I wonder why that is though.

BenLike what is Eric can sit down and implement uh a simple version of sends and receivers exactly with his eyes closed. A few hundred lines of code with his eyes closed. I have to say, in my implementation, I found, you know, there are some C isms that you have to kind of dance around a little bit, but the design of it is really very simple under the hood. And once you grow up the design, and once you just sort of get the hang of a few different implementation patterns, the implementation is really nice. It just sort of falls out very nicely.

BenUm it's not it's not difficult at all to in general to add new algorithms, to add new sort of ways of combining senders, that kind of thing. It all it all fits together very nicely in my experience.

ConorYeah, I know Eric, that's one of the things that uh when I've chatted with him is that you know he he shows this kind of base set of algorithms and senders. Uh but similar to how Stepanov is was always saying, you know, this is just the initial set, you know, there should be uh multiples more, you know, there should always be an underscore the the counted version of the uh underscore n versions. And there like there should there should be tons more, and uh don't just stop at what the standard library gives you. Is Eric is has said the same thing in in designing these and proposing them, is that this is just the core set, but on you know, it should give you the ability to write a world of asynchronous algorithms for all your different you know types of computation that you want to do.

ConorYeah, yeah. Which is exciting that we're getting it in the standard, because then hypothetically speaking, down the road in ten years, there should be a bunch of asynchronous algorithms that you'll have access to readily available in your uh standard library near you. Or fifteen years, however long it takes, though it can take time. And so have you um this or I'm not sure if we want to kick this because we've already gone past time a while. Maybe we should maybe we should do Sanders Deceivers part two, and you can talk about some of the algorithms that uh you've ended up designing.

ConorSure. Um because that's something that I uh I've spent very little time yeah working with.

BenYeah, yeah. We can put that on ice for today and uh talk about it in the future. It would be great if we could uh talk to Eric as well.

ConorYeah, I'll reach out. Um I know he is uh he's a busy guy. He's he's he's juggling like several things right now committee work, uh yeah, the internal implementation of stead execution, and now there's a whole new other C equivalent of a Python initiative that he's spearheading as well. So he's a he's a very busy guy, but I'm sure if I mention that we um want to chat about uh senders and receivers, I know he's given talks at CPPCon, and it's been one of his life missions to make this a more understandable topic. Because it it does for some reason seem to be a thing that it takes a couple times, including for me, for people to hear it.

ConorAnd I know that there's more and more folks. You are one of them. Uh is it Deepmar? Deepmar and there's a few other folks that like at first it was just Eric, and then now that I know that there's a handful of the R Yeah, I think Rob Leahy has been doing some stuff as well.

BenYeah, yeah, past guests on the podcast.

ConorSo it is becoming a more um, you know, uh well understood and uh, you know, uh topic. So part one in the books. Part two, maybe with maybe with Eric TBD. Uh yeah. All right. Well, I mean, I know we we had the schedule for to end 30 minutes ago, but I should just start booking these for two hours because uh I don't think we've had a single conversation where we we and you say in general I book off more time or like you know, uh in your in your head at least.

BenWell we end up recording what a month's worth of podcasts whenever we get together, something like that.

ConorI think this one this will probably be three, even though we have the time, the recorded time enough, but uh I feel like splitting up the uh SNR one into two would we'd you know that'd be uh doing our listeners a disservice because um I try my best because I get enough complaints about the uh slicing halfway through a topic and then they're like, ah, why do you do that? And a lot of folks I've realized they actually wait for like all four parts and then they'll listen like a month later and they'll listen to all at once. Okay.

ConorWhich isn't a bad strategy. Um I prefer, you know, as soon as things are out to listen to them. But um, but yes, if you if you just finished listening to this, I took the time to edit the extra 20 minutes. Um but yeah, we'll we'll do this again and uh I'll yeah, I'll reach out to Eric and see if uh and see if we can get him on. And uh to the listener, till next time. Be sure to check these show notes either in your podcast app or at adspthepodcast.com for links to anything we mentioned in today's episode, as well as a link to a get up discussion where you can leave thoughts, comments, and questions. Thanks for listening.

ConorWe hope you enjoyed and have a great day. I am the anti price. Um

Open the transcript file

Hosts and Guests

Nobody is credited on this episode yet.

Credit the people on this episode

Be the first to rate this episode

More episodes

All episodes
  1. Episode 313: C++26 Reflection vs CRTP, AI, Kids & More with Zach Laine

    Nov 20, 202647 minAudio

    In this episode, Conor and Bryce chat with Zach Laine about C++26 Reflection vs CRTP, AI, kids & more. Link to Episode 313 on Website Discuss this episode, leave a comment, or ask a question (on GitHub) Socials ADSP:...

    • Apple Podcasts

    Credit the people on this episode

  2. Episode 312: The C++26 Contracts Fiasco

    Nov 13, 202652 minAudio

    In this episode, Conor and Bryce chat with Zach Laine about the C++26 contracts fiasco. Link to Episode 312 on Website Discuss this episode, leave a comment, or ask a question (on GitHub) Socials ADSP: The Podcast:...

    • Apple Podcasts

    Credit the people on this episode

  3. Episode 311: Career Changes, GPUs & More with Zach Laine

    Nov 6, 202628 minAudio

    In this episode, Conor and Bryce talk with Zach Laine about his career and the career changes he has made. Link to Episode 311 on Website Discuss this episode, leave a comment, or ask a question (on GitHub) Socials...

    • Apple Podcasts

    Credit the people on this episode

  4. Episode 310: Leaving NVIDIA for... ?

    Oct 30, 202624 minAudio

    In this episode, Conor and Bryce talk about Bryce's next chapter and where he is leaving NVIDIA for. Link to Episode 310 on Website Discuss this episode, leave a comment, or ask a question (on GitHub) Socials ADSP: The...

    • Apple Podcasts

    Credit the people on this episode

  5. Episode 309: Default Construction & CppCon 2026 with Jason Turner

    Oct 23, 202629 minAudio

    In this episode, Conor and Ben chat with Jason Turner about default construction in C++. Link to Episode 309 on Website Discuss this episode, leave a comment, or ask a question (on GitHub) Socials ADSP: The Podcast:...

    • Apple Podcasts

    Credit the people on this episode