C
C#14mo ago
Gergő

❔ .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
PracticalPotato
PracticalPotato14mo ago
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.
cap5lut
cap5lut14mo ago
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
JakenVeina
JakenVeina14mo ago
whatever .NET deems most-optimal for the hardware
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
Gergő
Gergő14mo ago
I was qurious rather about the minimum size of the thread pool
PracticalPotato
PracticalPotato14mo ago
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
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
Gergő
Gergő14mo ago
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
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
PracticalPotato
PracticalPotato14mo ago
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.
Gergő
Gergő14mo ago
What is an os thread and a hardware thread?
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
Gergő
Gergő14mo ago
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
MODiX
MODiX14mo ago
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
React with ❌ to remove this embed.
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
Gergő
Gergő14mo ago
So hardware thread is a number 4? It just doesn’t make sense to me
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
chef drone builder
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.
Florian Voß
Florian Voß14mo ago
@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 😦
Gergő
Gergő14mo ago
The OS thread isn’t a physical thread then? So they’re like Promises/Tasks?
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX14mo ago
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
React with ❌ to remove this embed.
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
Florian Voß
Florian Voß14mo ago
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.
Gergő
Gergő14mo ago
And then the OS assigns the OS Threads to the logical threads you have
ahhhh, 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?
Florian Voß
Florian Voß14mo ago
If spawning is needed yes and possible (not reaching limits). If there are OS threads free to do work it doesn’t spawn any
Gergő
Gergő14mo ago
ahha I see I see
Florian Voß
Florian Voß14mo ago
If it reaches maximum thread pool limit and no more OS threads can be spawned it enqueues it and takes next available OS thread
Gergő
Gergő14mo ago
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?
Florian Voß
Florian Voß14mo ago
Never heard the term platform threads
Gergő
Gergő14mo ago
pthreads in C the physical threads I guess
Florian Voß
Florian Voß14mo ago
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
Gergő
Gergő14mo ago
I guess basically every modern cpu supports it, don't they?
Florian Voß
Florian Voß14mo ago
Yep .
Gergő
Gergő14mo ago
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?
Florian Voß
Florian Voß14mo ago
Yep That’s what TaskScheduler does
Gergő
Gergő14mo ago
so you can have 1000 tasks in your app sharing 10 os threads that maps to 1 cpu core
Florian Voß
Florian Voß14mo ago
Exactly You got it
Gergő
Gergő14mo ago
nicee fine thanks mate! 🍻
Florian Voß
Florian Voß14mo ago
🍻
Gergő
Gergő14mo ago
Can I somehow save/archive this post? I'd like to read it again a few times in the near future
Florian Voß
Florian Voß14mo ago
I don’t think you need to, a thread I have created in February still exists even tho I had closed it
Gergő
Gergő14mo ago
great, okay
Florian Voß
Florian Voß14mo ago
But copy and paste into txt file? 😅
Gergő
Gergő14mo ago
lol yeah, sounds like a solution 😄
cap5lut
cap5lut14mo ago
also dont forget to $close the thread if u dont have anymore questions ;p
MODiX
MODiX14mo ago
Use the /close command to mark a forum thread as answered
Gergő
Gergő14mo ago
Just a few question about memory as well. What is “native memory” and “managed memory”?
cap5lut
cap5lut14mo ago
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
Gergő
Gergő14mo ago
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.”
cap5lut
cap5lut14mo ago
yes for exampe by P/Invoking into native libraries
Gergő
Gergő14mo ago
Aah I see I see
Accord
Accord14mo ago
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.
Want results from more Discord servers?
Add your server
More Posts