In this episode, Conor and Ben chat about algorithms / schedulers in C++26 Senders and Receivers. Link to Episode 236 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-05-13 Date Released: 2025-05-30 C++26 Senders and Receivers C++98 std::count_if C++20 std::identity Louis Dionne's boost::hana Intel's C++ Bare Metal Senders and Receivers NVIDIA/stdexec (Senders - A Standard Model for Asynchronous Execution in C++) Rob Leahy C++Now 2025 Talk 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 a couple of rat other algorithms I have in my implementation. I have repeat, I have repeat n, repeat until, and that takes a sender and it runs it, and when it's done, it reconnects it and runs it again. I have retry, again, retry n and retry until, which does the same thing as repeat if the sender ends on the error channel. When the sender completes successfully, the retry doesn't do anything. But when the sender completes with an error, retry will we'll retry it.
ConorToday with my co-host Ben, we finish part two of our conversation about algorithms in C26 senders and receivers. Okay, this makes I mean this makes perfect sense. When all, when any. I'm not sure if you've explored because there are several implementations of um senders and receivers out in the wild. Do do all of them have are there a certain common, like I this is a very logical, like if you're gonna model when all, when any is a very natural step, does this continue to a certain extent of like there's these logical operations that even though it's not standardized, everybody's implementing under the same name or a different name?
BenI mean I think when any is a good example. I don't I don't know what other people have implemented, but I think I think when any is a sort of logical next step. Um the interesting design, one of the interesting design decisions about when all and when any is uh what do you do? So I think the standard requires that you pass at least one sender to when all. When any isn't standardized yet. But my implementation, I allow you to pass zero senders to when all, and indeed to when any.
BenThey're variadic functions, they could be they could have zero arguments. If you pass no sender to when all, what you get back is just uh is just, in fact. It completes immediately, right? You get back a sender that completes immediately because you've asked it to do when all over zero things. And and this also ties back to like the sender that completes immediately is the unit for the monoid of when all. Right? Just like true is the unit for the AND monoid. The sender that completes immediately, we would call it just, is the unit for the when all monoid.
BenConversely, if you look at when any, if you pass when any zero arguments, what does it give you back? And the answer in in my implementation is it gives you back a sender that never completes. It's equivalent of false, it's the unit for that monoid. Right? If you if you race that against any other sender, the other sender will always win. How is how do you actually program that? Uh you you you just never complete. You it's not it's not so it's not an infinite loop.
BenYou don't need an infinite loop, right? It's you you you literally never call set value. Your sender, you start your sender and it literally does nothing. Like it never makes progress. So the question that always arises is when would you want that sender? Right? And and as it so, as in so many such questions, it's difficult to answer in a vacuum. But as you know, when you start programming with these things, when you start composing these things, you can very well hit situations.
BenIt's the same sort of idea as why why is the identity function useful, right? It's the same sort of idea. You look at that first and you think, when would I ever want that? But then when you start programming in a compositional way, you think, oh, actually that is useful, I'll plug it in here. Um and I think it's the same sort of thing with the when any taking zero and the when all taking zero. You know, on the face of it, they they really do nothing. But but but two arguments. One, they they could well be useful, and two, I I really would I really don't like putting arbitrary limitations for things that will perfectly well compose.
BenYou know, it it's sort of like I don't want to go down the road of singling out like anyone who's done metaprogramming or knows that they need to handle void. You know, void is special, void needs its own special handling. I don't want to introduce special cases like that into the compositional framework that is senders in my implementation.
ConorOkay, yeah, that makes sense. I mean, that's a good uh analogy, because I I for one, that's the exact thought that I had when I first learned about identity was like, what? A function that does nothing? It just returns you what you gave it? Like, that's the most useless thing ever. And now there's been many times where it is exactly, you know, you have an algorithm that, you know, uh the tr I was trying to think of an example. Like it might be a bad one, but like countif is a thing in C. But what if you have a a vector of uh a vector of bool, a vector of booleans, or a container of booleans, and uh you want to count how many are true.
ConorWhat is the unary operation that you have to pass to countif?
BenRight, right. The projection function on many algorithms, the default is going to be the identity.
ConorAn even better example, right, yeah. So it is, it is, it does come up. And if my example's not great, there's been a ton of times, not so much in C, but in other languages, where there is an algorithm that's exactly what you want, but you don't need a uh transforming unary operation. You just you just need to count the Booleans or s or do something with the Booleans that are there.
BenUm it's more than just identity, right? It's all of the small birds. All of the small birds, right? They don't they make up the ecosystem.
ConorYeah, yeah. That's a good point. I mean, first, uh, which is the K combinator, that one also similarly, like if you think identity, a function that just does nothing and passes you back what you give it, uh, first also kind of seems in that similar boat. You give it two things and it gives you back its its first one. But I think that's a lot easier for people to grasp why that's useful, especially if we had a convenience function like that that worked on tuple types and pairs, because how often are you trying to do some transformation on a pair tuple and spelling that with a lambda where you're doing a dot second or a std colon colon get angle bracket zero zero, like a nice convenience combinator that did that for you would be would be very nice, which is what other languages um often have.
ConorAnd I think Boost HANA, uh Louis Dion's library, which uh at one point was new but is now a few years old, um it has all of that stuff because it was modeled after functional functional uh libraries and languages.
BenSo a couple of rat other algorithms I have in my implementation. I have uh repeat, I have repeat and repeat until. You know, they're just variations on repeat. And that takes a sender and it runs it, and when it's done, it reconnects it and runs it again. You know, keeps going. I have retry, again, retry n and retry until, which does the same thing as repeat if the sender ends on the error channel. Right? When the sender completes successfully, retry doesn't do anything.
BenBut when the sender completes with an error, retry will will retry it.
ConorOkay, that makes sense. But I think actually I just had a thought, and it is that and you can correct me if I'm uh incorrect about this, which I I might be. It sounds like there are buckets of these combinators or operations, whatever we want to call them. Some of them take as argument senders, and some of them take as arguments operations or lambdas. Yep. And all of them return senders?
ConorYes. All of them return.
BenIn general, they take a sender and maybe some other stuff, and they return a sender. They m they they modify a sender in some way. Some of them take multiple senders, like when all, some of them take a single sender, like then, and and and then they take other stuff as needed, which is according to what they are doing. Like then takes a lambda to run, right? Repeat N takes a size T, which is the N. Right.
ConorSo or I'm trying to think, it's like if you wa there's this there there's this uh mental model forming in my head where uh in of these com combinators, some of them are used for kind of the computation aspects of your graph, and then others are responsible for the Composing. Composing or path constructing of your graph. And really it seems like so far in my head, you have then and you have let value, and those are kind of responsible for the quote unquote computation.
ConorUm and then almost everything else so far has kind of been this graph constructing. You know, when all is got a many to one, when e when any is a many to one, repeat is a a one-to-one that kind of creates like a linked list, if you will. Repeat n linked list with a defined length. I'm not sure.
BenDoes that is that uh Yeah, you know, that you can think of it like that. I think that's I think that's right, definitely. There's something there. Um I was gonna say, again, something I've implemented which is not standard is um sequence, which is which is It's IOTA. No, no, it's it's sequencing two senders together. So it's like let value except that there is no data exchange from the previous to the subsequent sender, right? In Haskell, bind is spelt greater than greater than equals, right?
BenBut there's also another operator which is just greater than greater than, and that's what sequence is. So you just want one sender to run after another, but you don't want the second sender to depend on what the first sender sends. So does this return you two senders then? It r no, it returns you a single sender who's which embodies that sequence. So it's like let value except that there is no the the function, if you like, if you pass let value annullery function, or a function or a function which just ignores its arguments, would be a better way, right?
BenImagine you want to pass let value a function which ignores its arguments and just returns the sender you want to res you want to run next. And that's that's what sequence is, right? Because in a sense, the monad is too powerful, right? I don't need all of that power. I don't need all the power of let value. I don't need to make a runtime choice. I just know that I want to run sender B after sender A.
ConorIs this for some like side effect y thing? Exactly.
BenYeah, my world, everything is side effects. Okay.
ConorI was gonna say is uh the only thing I could imagine that you're trying to write to some screen or something like that, and like you know that you want this to happen first, and then the other thing to happen.
BenAnd the reason why sequence is useful is because it doesn't have that runtime choice, right? Because effectively, when you have a runtime choice, you are putting a compile-time barrier there that that that you cannot cross with type information because you have the runtime choice. Like downstream of that, you don't know the type information from upstream because it's all changed at runtime. Um but when you don't have a runtime choice, then you have the opportunity for more type information to propagate across that boundary.
BenRight. That makes that makes a lot of sense, yeah.
ConorOkay.
BenSo another overload of uh of sequence. So yeah, sequence sequence, and it's just like let value where you ignore the arguments, but but the reason it's formulated differently is because of that type propagation.
ConorAnd is there uh not that uh our our C listeners care, but uh is there a name for that in Haskell, other than the operator?
BenUh I don't know how to pronounce the operator, but but the so I don't know. Um I think I think it might be even pronounced seek or sequence, one of those two. Okay. Um and then so the one so we've talked about senders. Another important thing to talk about is schedulers, right? Because schedulers produce senders. They're they're they're the entry point, apart from just, which is which is producing a sender. Um schedulers are the are the main way you you get your entry point into your computation, right?
BenAnd the scheduler just represents where work is going to happen, or some some execution resource, and and you you it g it can give you a sender, right? You call schedule and it gives you back a sender. And that sender represents the entry point into computation on that on that resource, right? So you can have a thread scheduler, you can have a thread pool scheduler, you can have uh uh, in my case, uh an interrupt shed, right? An an interrupt scheduler. So I have several. I have um I have a timer scheduler that gets that hooks up to a time interrupt.
BenI have a priority scheduler, which hooks up to a priority interrupt, a given priority level. Uh, and then I have a trigger scheduler, which so so time timer is is one piece of hardware, priority stuff is another piece of hardware, another in more interrupts. But there are tons of other interrupts in general, and for the generic style interrupt, I have a trigger scheduler. So I can I can give a trigger trigger scheduler a name, right?
BenAnd then in an ISR, in an interrupt service routine, when that interrupt is called, I can say, run that trigger scheduler. You know, this is the trigger by name, right? And that when that interrupt happens, therefore, that means that now I'm running in that interrupt context, and I'm running whatever sender chain was hooked off of that trigger scheduler. So that's the way I can hook up in general. That's the way I can hook up work to interrupts in the senders model.
ConorDo you have a uh example of where these kinds of things are interacting in some kind of I guess embedded context is what you're working in?
BenUm trigger scheduler has been just about the most useful thing I ever came across in this sense. So so you know, there's there's the the time scheduler is one thing where you can say, give me a scheduler for 100 milliseconds in the future, right? And when you when you start that when you start that operation state, when you start the computation, it goes away and it and it sets the timer interrupt. And when the timer interrupt fires, it will it will run you, right? So that's a timer scheduler.
BenThe priority scheduler works very similarly, except instead of setting the timer interrupt, it sets a uh priority interrupt, which if it's higher than your current priority, probably means you get interrupted immediately because the higher priority takes over. Um, those two are you know very, very common to think about across a wide range of hardware. Practically every embedded system has those two, right?
ConorRight.
BenWe could say, I don't know about practically every, but but many, many, many, you know, many people that have used to think about those. The trigger schedule is for all other interrupts, so any kind of ad hoc interrupt you have. So, you know, if you're what in my case, I'm working with the hardware architects, and we can choose to add in interrupts for various things, right? And and in fact, you know, if you pick up any embedded system, you'll see it has various other interrupts. All of those are sort of ad hoc. And I, you know, the trigger scheduler takes care of pretty much all of them.
BenYou just make a different name for each one when you instantiate the trigger scheduler. And now you've got a custom scheduler for that interrupt. Right. So like you might get an in you might, you know, you might send something off on a bus and get an interrupt when the r when when the response comes, you would hook that up to a to a named trigger.
ConorAnd these uh clearly are essential for putting everything together. Yes. Yeah. You're not going to be able to do much with just. Right, right.
BenAnd and the extra thing that trigger scheduler does is it allows you to send arguments. Like, like just. You know, just takes arguments. Ordinarily, when you have a scheduler and you call schedule, it gives you a sender that sends nothing, right? It just calls set value with no arguments. Um but trigger scheduler, when you trigger it, you can give it arguments. So it behaves a bit like it behaves like just, but in the context of that of that triggered resource. You know, the other interesting part that sort of asynchrony brings into focus for me is that um, you know, it and you can correct me, but when but when but when you're thinking about, you know, doing computation, and you're thinking about even sort of using parallel algorithms or something, typically you are thinking about the computation as somewhat monolithic.
BenYou're not thinking about other things happening while you're doing the computation. You know, you might be thinking about splitting it up for parallel use, but even then you're thinking, well, I own the cluster, I own the CPUs that it's running on, whatever I'm doing. I'm gonna split it up for for for performance. Um but but the other thing, think about it in terms of asynchrony, is like it helps to split tasks up into small pieces, right? Even though there's a little overhead in scheduling the pieces, even though were I able to do this task monolithically, it might finish sooner, right?
BenBut by splitting it up, I'm trading off a little bit of time, not a lot of time, but a little bit. Um, but I'm also allowing uh I'm allowing the computation to be cancelled, I'm allowing other things to get in there, I'm a I'm I'm being a good citizen in in the wider world of of scheduling things, right? I'm not tying up resources until I'm done. And that is also very important in the embedded world.
ConorYeah, I mean that makes uh a ton of sense to me. I mean, I think about a lot of the code that I've written, the topic that comes to mind is uh my Scrabble game. And there's a lot of like you when you write code, you're writing it in a sense serially on uh on a page, right? But one of the the first times I the first pass at you know getting the word solver in that game, you've got a 15 by 15 board and you need to explore starting positions for different words.
ConorUm but there's a lot of stuff you can think about, like, and that that's what I ended up building into a data structure, but you could have done some kind of asynchronous thing where you're exploring a word try, um, and you want to, you know, at a certain point you know, okay, there's there's no more words after this. But like the naive way to do it is just create every single combination and look it up in a dictionary. Uh, but that proves to be just a combinatorial explosion that just you know halts. And I I even tried doing that and then creating some kind of thread pool and dispatching, you know, 64 threads, but even still, it's just it's just absolutely awful.
ConorSo you end up encoding kind of the asynchronous, you know, if you you could code it that way. It probably might end up still being less efficient than you know using your your word try at the end of the day. But there's a lot of stuff that like if you're searching for results, you end up building up that list and then sorting it and whatnot. But you could picture a kind of asynchronous algorithm where it's popping up and dynamically, you know, you have the top five words and then it's it's inserting them in and you get some live dashboard of like while it's searching the space, um, it's showing you, you know, iter i incrementally or iter iteratively when it finds a uh a good solution or good play.
ConorUm but currently, you know, I don't have that any of that built into the game because I don't I don't use asynchronous algorithms in um, yeah.
BenAnd when you when you run when you run a regular synchronous algorithm on the desktop, you you sort of don't, you know, you you're running on an OS. So unknown to you, you get you get, you know, underneath under the hood, you get the OS deciding when you can run. You get preemption, you get, you know, resource allocation by the OS. But when you're running on bare metal, you don't have any of that. And so, you know, running a monolithic thing means you are tying up resources for that amount of time. And this sort of ties back also, reminds me of um, you know, I spent ages in games, so a lot of times everything comes back to me mapping it back in my mental model to how games work, you know.
BenAnd it's sort of, you know, in games you are trying to hit your 60fps or whatever it is, but you're trying to hit the frame rate. You're you're you're not you're not you don't want to tie up resources because that would mean that you overshoot your frame time or whatever. I mean, these days games run on on machines that maybe deal with that a bit better, but you know, 20 years ago, this was certainly a concern. I think it's still a concern today, but you know, you know, so it's things like if you have a very expensive thing to do, uh you have to structure your computation so that you can do it over multiple frames.
BenYou have to make it so you can pick up where you left off. You know, you you you have to not do it all in one go because that would take you know half a second or whatever. You know, if you're doing some kind of pathfinding or or something computationally intensive like that, um you you build it so that it can happen piecemeal, right? And it's and it's somewhat the same idea that comes into structuring asynchronous computations, at least at least for me. I sort of map it to that.
ConorYou literally, when you were saying that, like five seconds before you said the word pathfinding, that's what I was thinking in my head too, as like another example. The City Stride root run you know uh run route you know uh building and trying to figure out how to do like a Monte Carlo tree search where you set the number of stats that it looks ahead so that you have an efficient algorithm, but that you know is finding an optimal you can imagine one where you set some condition that as long as one of the paths that it's currently being explored hits some threshold, just target now, now reset the search from there because I know you know that in practice as long as I can find this metric it goes.
ConorBut like currently that's not the way my algorithm works. It's just a uh I guess it's a a breadth-first search that does a certain number of steps, but it always completes, regardless of like how bad that route is gonna be for n steps, right? Um and you could imagine, once again, like uh an asynchronous version of this that says as soon as you know when any of these paths uh meets this criteria, go from there. Um yeah, I could see yeah, I can I can picture a bunch of different pathfinding stuff that is this kind of stuff is very useful.
ConorYou're already using this on a daily basis, or is it daily? I'm not sure.
BenYeah, you could say that. Maybe not every day that I have to dip into it, but certainly it is getting used, yes.
ConorYeah, and then other yeah, DeepMar, you mentioned last episode, or maybe it was two episodes.
BenYeah, I mentioned him earlier. He gave a talk at Super Plus now. Yeah.
ConorAnd uh I mean it sounds like yeah, Rob's using it as well. So this is slowly, even though only it's um would you say partially standardized? I guess the foundations are standardized and going forward there's gonna be more algorithms that are are standardized. I mean, sim not not dissimilar to how other um library things like ranges have entered the standard and then the API surface has grown. Right. If folks are listening to this and, you know, they're potentially working at a company or have a personal project, is this um I mean, so you've gone and built your own.
ConorAt what point, I guess C26, folks will be able to reach for this, so that means like what, C plus plus thirty or or twenty thirty. Um it's a library feature, so but potentially it might actually come quicker than some of the uh I guess tooling stuff.
BenIt's a bit of work, but um, you know, the library implementers are uh up to the job, I'm sure. Not quite sure when they'll get around to implementing it, but coming to a standard library near you soon.
ConorAnd also it's is is the Intel one is that I assume that I assume that's a good thing. A lot of the stuff on GitHub, it's open source. So license Would you say that that is uh is designed primarily with embedded in mind or is it generic it's designed with embedded in mind, it is non-standard.
BenIt it it it f it obviously follows the same ideas as the standard and the sort of you know the in terms of parts of it, but it's it's not at some point it diverges.
ConorAnd I know for that matter, NVIDIA has one as well. Eric wrote it. I believe it's called libs.exec. Yeah, there's a ton of implementations out there. We will also once again link to Rob's talk. And I think I saw that there was also a couple other, and you mentioned there was a couple other senders and receivers, if not specifically adjacent talks at C now.
BenYeah, there are a few talks out there in the wild already now, and and more to come, I'm sure. Awesome.
ConorAny final final uh words of wisdom for the listeners?
BenWear sunscreen. It's it's spring, the sun is out in force, especially here in Denver where we've got a bit of altitude. It's important to wear sunscreen.
ConorIt's funny you mentioned that because my uh my fiance might enjoy the LLMs and AIs even more than I do, and she's taken she has you know AI assisted everything now, including um recommendations for skin routines. And she's tick took a photo of me and fed it to uh ChatGPT and got recommendations, and of course it told her her skin was glowing and fantastic. She looked younger than you know her age. And for me, it said, you know, this is clearly uh early signs of aging, etc.
ConorAnd so now she has me on a skincare program that involves putting uh some moisturizer cream with suntan lotion every morning, even if I don't plan on going outside. It's it's uh anyways.
BenThe fact that you said suntan lotion speaks volumes. Or what did I Suntan lotion is what we used to call it in the 70s, I think. Sunscreen is what well it's it's not helping uh well, it depends what your point of view is, but but the idea is less to tan and more to protect yourself, I think, these days.
ConorI mean I I it's it's a speako there. Uh uh although I think sun lotion, sunscreen, sunblock, they're they're all good. But honestly, I I'm not I'm the last person in the world you should ever I I put her in charge of my skin routine, and if she's not reminding me, you know, I I forget. So um she's concerned about my my early aging. Uh I we can't all look as young as you, Ben. I don't think uh You're a programmer.
BenAll you have to do is just stop going outside. It's very natural.
ConorThat's the secret to aging well, is just don't go out much. I really like being outside though. It's nice to be outside. Uh but listen to Ben. Listen to my fiance, wear sunscreen, or block, or lotion. Be sure to check these show notes either in your podcast app or at adsphepodcast.com for links to anything we mentioned in today's episode, as well as a link to a GitHub discussion where you can leave thoughts, comments, and questions. Thanks for listening. We hope you enjoyed and have a great day. I am the anti Bryce. Um
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:...
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:...
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...
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...
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:...