Clarification on thread pool max threads

12,977

I have looked at source code and have found that default value for MaxWorkerThreads is set to 100

private static readonly ConfigurationProperty _propMaxWorkerThreads = new ConfigurationProperty("maxWorkerThreads", typeof (int), (object) 100, (TypeConverter) null, (ConfigurationValidatorBase) new IntegerValidator(1, 2147483646), ConfigurationPropertyOptions.None);

This field is added to properties collection in static constructor

ProcessModelSection._properties.Add(ProcessModelSection._propMaxWorkerThreads);

In property definition they do set default value to 20

[IntegerValidator(MaxValue = 2147483646, MinValue = 1)]
[ConfigurationProperty("maxWorkerThreads", DefaultValue = 20)]
public int MaxWorkerThreads

But this obviously give no effect. Maybe it's some kind of legacy implementation. By the way it behaves this way only if autoConfig is set to false. When it's set to true I have 32K worker threads in my application. Probably this behavior depends on IIS version.

Share:
12,977

Related videos on Youtube

Royi Namir
Author by

Royi Namir

Updated on September 16, 2022

Comments

  • Royi Namir
    Royi Namir over 1 year

    I've read here that :

    In v2.0, 3.5, and 4.0, ASP.NET initializes the CLR ThreadPool with 100 threads per processor(core)

    That is correct , I checked it (I have 8 core machine , so 8*100 = 800):

    enter image description here

    But then I saw this and this:

    maxWorkerThreads — Configures the maximum number of worker threads to use for the process on a per-CPU basis.The range for this attribute is from 5 through 100. The default is 20.

    Question

    I don't see how the numbers fits in here :

    The first paragraph states that I have max 100 threads per core ( the image prove it , I have 8 cores).

    But the second paragraph states that the default maximum worker threads per core is 20. So if I have 8 cores then I must have 8*20 = 160 max threads. not 800.

    Can someone please shed light?

    Update:

    I just found a way to get the key element value via c# code :

    enter image description here

    So now the number are fit in ,but still - MSDN say the default is 20 , not 100

    enter image description here

    And then they do mention 100 :

    enter image description here

    What is going on here?

    • hackp0int
      hackp0int almost 10 years
      @HenkHolterman Can you explain it further more with a deeper explanations.
  • Royi Namir
    Royi Namir almost 10 years
    please read it again. request != thread. asp.net sets 100 threads per core. this attribute is for async operations
  • cjcurrie
    cjcurrie almost 10 years
    @RoyiNamir, perhaps I misunderstand. .NET 4 in IIS 7 has 5000 max concurrent application pool threads per CPU, as reported on this page of the MSDN
  • Royi Namir
    Royi Namir almost 10 years
    5000 max concurrent requsts ! not threads ! Again , thread != request. this is for async operations where a request doesnt tie a thread. (The difference only matters when the requests are asynchronous (the request either has an asynchronous handler or a module in the pipeline completes asynchronously). read this excat line here blogs.msdn.com/b/tmarq/archive/2007/07/21/…
  • Royi Namir
    Royi Namir almost 10 years
    So the docs are old updated via attribute and not via property ?
  • Andrii Litvinov
    Andrii Litvinov almost 10 years
    Could be, I actually have no idea.
  • Royi Namir
    Royi Namir almost 10 years
    p.s this whole uqestion came from this book chapter which I read i'll be glad if you have a look at the test he did there - which I don't understand : if each core has 100 threads to work , why does in the first 10 seconds - there were only 13 threads used ? why not more ? ( he did there synchronous operation which tie a thread)
  • Royi Namir
    Royi Namir almost 10 years
    (continue)... but still - he says that threadpool create 2 more threads each second. but why create threads if asp.net already have 100 threads READY TO GO !!!! ? can u have alook at it ?
  • Andrii Litvinov
    Andrii Litvinov almost 10 years
    Actually ASP.NET doesn't have 100 threads at start time. ThreadPood creates new threads when requested up to minWorkerThreads value. Then if there are still work to do and threads are not enough it will add new thread each half a second when needed.
  • Royi Namir
    Royi Namir almost 10 years
    no. asp.net does have 100 threads per core at start time. i.stack.imgur.com/RvJQ3.jpg ( this guy build iis core)
  • Andrii Litvinov
    Andrii Litvinov almost 10 years
    Try to check yourself by calling ThreadPool.GetMaxThreads and ThreadPool.GetAvailableThreads at application start time or when request is executed.
  • Royi Namir
    Royi Namir almost 10 years
    My friend , I already have. ive been working on it for the last 5 days. look here and see how initializations threads are diffrent and how asp.net does set 100 threads per core i.stack.imgur.com/vbQhD.jpg
  • Andrii Litvinov
    Andrii Litvinov almost 10 years
    ThreadPool.GetMaxThreads doesn't give you number of threads created in thread pool. It's a number of threads that it is possible to create. Try check you app memory footprint. Each allocated thread takes about 1MB in memory. I don't think you app consumes 1GB at start time.
  • Royi Namir
    Royi Namir almost 10 years
    if so , if i have 200 users who requests data from the server , they will wait a long time till threadpool will have enough threads( assuming prv threads still working)....
  • Andrii Litvinov
    Andrii Litvinov almost 10 years
    Exactly. You can set minWorkerThread to 25 for your 8 cores server if you know for sure that it will use it. But it is not recommended because if the load will fall down thread will still be in use but not that effective. Try read this article too.
  • Royi Namir
    Royi Namir almost 10 years
    Thank you my friend. my mistake was missing the "max" word. I thout they are initially initialized to 100 threads. appraently they initialized to MAX 100 ( per core). however i still wonder how iis (8 core) takes care of many many simultainously requests ( ~2 sec each) while growing its thread pool threads by 2 per second.....
  • Andrii Litvinov
    Andrii Litvinov almost 10 years
    Sure, I send good time digging IIS and understanding how it works. I think for that many requests IIS maintains other queue. AFAIR this was described in those two articles from Thomas Marquardt. (One you've mentioned in you question).