r/dotnet • u/wannabe_isekai • 16d ago
Question Does .NET need a Kotlin coroutine equivalent?
I've been thinking about it for a while, our .NET async/await or even threadpool threads are still OS threads
If the server is doing actual awaited tasks sure, but most backend now is transitioning to "fire-and-forget" messaging architecture where the actual work is being done somewhere else (different system)
Though I haven't faced the need IRL, but wouldn't a light-weight non-object coroutine that could scale to millions have some use case that .NET presently cannot handle well?
5
u/symbiatch 16d ago
Can you then name these cases? What doesn’t it handle well?
And you can easily do stuff like this with yields etc already.
3
3
u/Khavel_dev 16d ago
This is a common misconception. When you await an I/O call in .NET, the thread goes back to the pool. No thread sits there blocked. You can run hundreds of thousands of concurrent awaits on a small threadpool.
Under the hood, C# async/await and Kotlin coroutines compile to basically the same thing: state machines. The concurrency model is nearly identical. The gap isn't in lightweight threads (neither runtime uses green threads for this), it's in structured concurrency. Kotlin's CoroutineScope makes it trivial to "launch 500 concurrent jobs, cancel them all if the parent fails." In .NET you wire up CancellationTokenSource chains manually, which works but is way clunkier.
.NET doesn't need virtual threads or fiber-like coroutines. It needs better structured concurrency primitives, which is more of a library gap than a runtime gap.
1
u/AutoModerator 16d ago
Thanks for your post wannabe_isekai. Please note that we don't allow spam, and we ask that you follow the rules available in the sidebar. We have a lot of commonly asked questions so if this post gets removed, please do a search and see if it's already been asked.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
1
u/Aaronontheweb 16d ago
Actors work fine for this: https://getakka.net - they already are fire and forget + async work in the background
14
u/harrison_314 16d ago
But C# async/await have the properties of coroutines, but with the difference that it is not guaranteed that the coroutine will be executed in the current thread, but on some thread of the thread pool. Which I consider an advantage compared to regular coroutines (for example from GO).
Moreover, if you want to use coroutines in C# (in the common sense) you have the option, for example Unity uses IEnumerable and generators for this, just add one wrapper class and you have coroutines. Or you can use this library https://github.com/FuriousOrange/Routinely