Matt Cashatt
Matt Cashatt

Reputation: 24208

How many threads does a .NET web application natively use?

This has been bugging me for a while. Assuming that I am not using any explicit form of task parallelism (e.g. Paralell.ForEach), how many threads are natively used when I deploy a web application to a Windows Server?

In other words, if the web server has an eight-core processor, does the application only use one of those cores if I am not explicitly telling it to use more?

I'll bet I have missed something simple here, so flame on--but I still want to know the answer. Thanks.

Upvotes: 4

Views: 4717

Answers (2)

Erik Funkenbusch
Erik Funkenbusch

Reputation: 93434

First, you have to consider that a web server is, by its nature, a multi-threaded system because it has to be able to respond to multiple requests at the same time (there are ways to do this in a single threaded environment, but it's not very efficient, and IIS does not support it). If two users access the site at the same time, two separate threads are servicing each request in most cases.

Once you get into scalability issues, and async support, then a thread can actually service multiple requests (or rather, a request can be put on a queue and the IIS worker threads can be reused to process other requests).

So you are looking at this from the aspect of a single user. And that single user will likely run only a single thread of activity during that web request (ignoring async, io completion ports, etc..). i.e. you can think of a single web request as being equal to a single thread in the general sense (but there are so many things that can spin off other threads, that this isn't something that you can really count on).

When you use something like Parallel.ForEach it's so that your single users request can now execute things in multiple threads to improve efficiency.

An IIS web site has a worker process associated with it. That worker process can be configured and tuned to control how many threads it uses. But just remember, that your web site is running under the control of IIS, not as it's own application.

Upvotes: 8

Raja Nadar
Raja Nadar

Reputation: 9489

The topic of threads can get really confusing as you move along .NET versions.

But to over-simplify, it depends on your processModel configuration for ASP.NET. (machine.config) autoConfig=true/false etc.

at a bare minimum, in .NET 4.0, the min/max WorkerThreads and min/max IOCompletionThreads determine the amount of threads in play.

e.g. If the minWorkerThreads = 1 (default value), then the total minThreads = 1 * no:of cores.. same with maxWorkerThreads.

Based on the load, the worker process can ramp up the threads needed to service the requests, using an undisclosed algorith.. minWorkerThreads to maxWorkerThreads. (of course based on availability of ThreadPool threads etc.)

Upvotes: 2

Related Questions