❔ .net web api thread pool size
I’ve been confused lately about this topic. So acutally, you how many platform threads do we have when starting a net application? It’s the general threads=cores? So on a 4core machine, 4 threads?
54 Replies
threads aren’t processes, so there isnt really an upper limit on how many threads you can use.
the only real limit is the capacity of whatever hardware you’re using to process those threads. (i.e. if your hardware chokes on trying to process too much at once and all the threads slow down) which usually isn’t an issue, but if it is, you generally just use benchmarks to figure out what the acceptable limit is.
especially in web api/async context the thread number isnt that relevant. just make sure to keep ur IO async
u can read more about that here: https://learn.microsoft.com/en-us/dotnet/standard/threading/the-managed-thread-pool
whatever .NET deems most-optimal for the hardware
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
I was qurious rather about the minimum size of the thread pool
well you could have a thread pool of 0. you just wouldn't be using threads.
you could have a thread pool of 1. but at that point you wouldn't need a pool.
etc etc.
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
What? What’s it then?
But when you start the app you must have at least one thread to execute the code
So how could be the pool size 0?
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Sure, if you want to think of it that way. I was talking about creating a thread pool.
When you create a thread pool, you could create a pool with size 0. It just wouldn't do anything.
What is an os thread and a hardware thread?
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
I read it but actually don’t really get that.. I’m confused
Os thread is a thread that gets executed in a core
Hardware thread is a thing at all? What is that
tebeco#0205
I'm saying an OS thread is not a hardware thread (assuming that hardware thread is what logical core count is ... due to hyper threading)
Quoted by
<@!689473681302224947> from #.net web api thread pool size (click here)
React with ❌ to remove this embed.
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
So hardware thread is a number 4?
It just doesn’t make sense to me
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Imagine your processor has only one core. But you still want to run multiple programs at once. Thats where a OS thread comes into play. It cant actually execute two instructions at the same time, the OS just switches between executing instructions from different OS threads. So when we talk about thread pool threads, we are talking about OS threads.
@Gergő most of that has probably already been said but here is my approach of explaining:
The cores of CPU define how many physical threads you have. an 8 core cpu has 8 "physical threads". Since most modern CPUs support Hyper-Threading, we get the same amount of "virtual threads" on top, which then makes 16 "logical threads" in total. That means our device can run 16 things in parallel.
But then there are also OS threads, which you can have much more of. I have for example 5000 active OS Threads right now, which you can see in Task Manager.
Those pretty much represent Units of work, much like Tasks in C# that get assigned to the 16 logical threads we have available. Windows does this so fast that all 5000 OS threads seem to be running in parallel.
sorry, I tagged the wrong person 😦
The OS thread isn’t a physical thread then? So they’re like Promises/Tasks?
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
kocha#3156
Imagine your processor has only one core. But you still want to run multiple programs at once. Thats where a OS thread comes into play. It cant actually execute two instructions at the same time, the OS just switches between executing instructions from different OS threads. So when we talk about thread pool threads, we are talking about OS threads.
Quoted by
<@!689473681302224947> from #.net web api thread pool size (click here)
React with ❌ to remove this embed.
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
1. No as TeBeClone said
2. I contradict here with @tebeco and say yes. OS threads are more or less equal to Tasks in C#, just that they don’t have 1 to 1 relationship because the TaskScheduler does lots of optimizations when assigning Tasks to OS Threads. And then the OS assigns the OS Threads to the logical threads you have (physical ones plus virtual ones due to hyper threading) and also does lots of optimizations there. All that happens so lightning fast that it appears you could run 5000 threads in parallel even if you have not many cores.
And then the OS assigns the OS Threads to the logical threads you haveahhhh, that was the missing point I missed this message, that's actually pretty great and informationful. So the Thread api in java/c# or any multithreaded language basically communicates with the OS to spawn more OS threads for us that get executed on the platform threads assigned to the proccess?
If spawning is needed yes and possible (not reaching limits). If there are OS threads free to do work it doesn’t spawn any
ahha I see I see
If it reaches maximum thread pool limit and no more OS threads can be spawned it enqueues it and takes next available OS thread
and how are the platform threads structured/organized? I mean, if we got 4 cores, we can run 4 things exactly at the same time. Right. But what happens when we increase the number of threads? Or basically we have as much platform threads as many cores at top and then the os just spawns the os threads if needed?
Never heard the term platform threads
pthreads in C
the physical threads I guess
Yeah you have those. And then if hyper threading is supported by cpu you also have virtual cores / threads on top as explained before. Sum of that makes logical cores / threads. Those are the terminologies I know
I guess basically every modern cpu supports it, don't they?
Yep
.
yeah I'm just reading that
great, I get it then
and another abstraction above os threads are tasks/promises provided by the application itself to use the os threads even more effeciently
right?
Yep
That’s what TaskScheduler does
so you can have 1000 tasks in your app sharing 10 os threads that maps to 1 cpu core
Exactly
You got it
nicee fine
thanks mate! 🍻
🍻
Can I somehow save/archive this post?
I'd like to read it again a few times in the near future
I don’t think you need to, a thread I have created in February still exists even tho I had closed it
great, okay
But copy and paste into txt file? 😅
lol yeah, sounds like a solution 😄
also dont forget to $close the thread if u dont have anymore questions ;p
Use the
/close
command to mark a forum thread as answeredJust a few question about memory as well.
What is “native memory” and “managed memory”?
managed memory is ... managed by dotnet, basically all heap allocation/deallocation happens there.
so if an object instance isnt referenced anymore, the GC will eventually release the memory used for it.
native memory is "normal" memory, the (de)allocation is done by urself
Like raw mem allocation in C is the “native memory” because it’s not managed by anything but the dev?
And how can a c# app have native memory?
Just trying to understand this:
“Unlike an array, however, a Span<T> instance can point to managed memory, native memory, or memory managed on the stack.”
yes
for exampe by P/Invoking into native libraries
and there is also https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.nativememory?view=net-7.0
NativeMemory Class (System.Runtime.InteropServices)
This class contains methods that are mainly used to manage native memory.
Aah I see I see
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.