Add easier ways to achieve concurrency without parallelism #99919
Unanswered
aaron-manning
asked this question in
Ideas
Replies: 1 comment
-
Threadpool can and will downscale the worker thread count based on the nature of the workload executed by .NET. It will be just one, maybe two threads, if the nature of the code being executed is purely single-threaded. Keep in mind that there will be other threads, namely:
With that said, with Workstation GC you will observe that the lowest threadcount for console application which does use async but in a single-threaded fashion will be 4. It does, however, take some time for the threadpool to scale down to this number. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Unless I'm mistaken, there is currently no way to configure .net to run with less threads than there are logical processors.
We can use ThreadPool.SetMaxThreads to increase the number of threads, but we cannot set it below the number of logical processors available (which is often 2x the number of physical cores).
There are a few workarounds, for example creating a custom TaskScheduler or a custom SynchronizationContext.
I can't see a way to make these perform as well as the default TaskScheduler, as it seems to utilize internal functions to reduce heap allocations, or has special treatment from the runtime in some way.
Here's a benchmark to demonstrate:
And my benchmark result
There should be no additional heap allocations in the custom TaskScheduler shown above, compared to the default, but that is clearly not the case.
It would be great if .net included a customizable high performance TaskScheduler so that we don't have to incur additional allocations to reduce parallelism, for example:
Or alternatively, provide public API's so that the same performance (reduced allocations) as the default task scheduler can be achieved in user code.
Writing concurrent code without fear of the issues that come with parallelism is one of the reasons NodeJs is so popular, it would be great if it were a bit simpler to achieve in .net as parallelism by default is often overkill and comes with many footguns.
Beta Was this translation helpful? Give feedback.
All reactions