No, No, No - Tasks are not threads and they are not for performance! #38
Replies: 3 comments 11 replies
-
|
Can you talk about Multithreading like this? Also Task.WhenAll seem to improve performance but I won't say they are on a single thread |
Beta Was this translation helpful? Give feedback.
-
I see this said a lot, but I think it leads people to the wrong conclusion. Technically it's correct, ASP.NET Core doesn't have a SynchronizationContext (I wouldn't say "modern frameworks" because MAUI for instance is modern and has a SynchronizationContext) but you cannot conclude that "whether you append ConfigureAwait(true|false) or nothing does literally make zero difference". Just because the ASP.NET Core framework doesn't setup a SynchronizationContext doesn't necessarily mean your code doesn't run under one, especially if "your code" is a library. Anyone can create a SynchronizationContext and call SetSynchronizationContext to establish one. So unless your code can somehow ensure that no where in the calling stack is that done, it may well run under one even if you're using ASP.NET Core. It's dangerous to assume there is no SynchronizationContext. |
Beta Was this translation helpful? Give feedback.
-
|
This article says pretty much the exact opposite of the official documentation here: In particular, around the 4th paragraph is this quote (emphasis mine):
I suggest you load the 3rd code sample in Linqpad and run it. If you add a small sleep here to simulate a non-zero amount of work being done by the task, you will see that the each task runs on a separate thread. Every. Single. Time.
Output: |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
No, No, No - Tasks are not threads and they are not for performance!
I often read that Task is used for multithreading in C# / .NET, but that is not the case. And it is crucial to understand why this isn't the case. We will also see which problem exactly Task is solving in the first place.
https://steven-giesel.com/blogPost/d095383f-7ea9-4419-96b8-889c6981cce0
Beta Was this translation helpful? Give feedback.
All reactions