Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

async/await is not for CPU-intensive parallelism. I think that's pretty much stated in the .NET docs. That's why Parallel Compute APIs like Parallel.ForEeach/For are not async. Their purpose is to enable non-blocking waits for IO, as well as to do stuff like animation on UI where you might want to execute procedural code over a larger timeframe.


The other reason those Parallel methods did use async/await is that async/await did not exist in .NET at the time those methods were introduced.

But good news! The upcoming .NET 6 release will have a Parallel.ForEachAsync method:

https://docs.microsoft.com/dotnet/api/system.threading.tasks...


Doing a bit of .NET archeology we find that both Task<T> and Parallel.For can be dated to .NET 4.0 So if they wanted to, they could've included async/await support. It just didn't make sense.


Async/await were officially added to c# 5 and .net framework 4.5

I think it was possible to have async/await in .net framework 4.0 via some workarounds when it was still in CTP mode but I don't recall the details.


Yes it is. Async is avoid blocking operations, whether it's IO-bound or CPU-bound. There are plenty of cases where computation can be offloaded to async tasks (eg: keeping the UI responsive).

The official docs even give examples to clarify both scenarios: https://docs.microsoft.com/en-us/dotnet/csharp/async




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: