Bug 7055 - Threadpool SetMinThreads only creates one new thread
Summary: Threadpool SetMinThreads only creates one new thread
Alias: None
Product: Runtime
Classification: Mono
Component: General ()
Version: unspecified
Hardware: All All
: --- normal
Target Milestone: ---
Assignee: Paolo Molaro
Depends on:
Reported: 2012-09-10 22:07 UTC by jake
Modified: 2017-07-13 00:01 UTC (History)
6 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description jake 2012-09-10 22:07:30 UTC
Description of Problem:
When I call ThreadPool.SetMinThreads it appears that mono only creates one new thread regardless of the delta between the number of existing thread pool threads and what I've requested.  I have to run that command in a loop to get the full number of threads.  Additionally, it appears that every .5s it will only increase the thread pool by 1, regardless of the load factor.  This can lead to a situation where a highly variable workload will get stuck for long periods of time after the load increases instantaneously.

Steps to reproduce the problem:
1. Call SetMinThreads(200, 100) and then use 'top -H -n 1 -b | grep mono | wc -l' to count the number of threads.

Actual Results:
Very few threads.

Expected Results:
Very many threads.

How often does this happen? 
Every time.

Additional Information:
Here is the code that appears to only create 1 new thread when you call SetMinThreads:

I haven't verified the slow increasing number of threads is capped at 1, but that could possibly be argued as a correct behavior anyway.
Comment 1 Ben 2013-03-17 05:55:02 UTC
I have compared this to .NET 4.0.  This behaves rather differently to mono 3.0.1 Linux and OSX.

Using worker threads, with a max and min set to 10, on a 4 core platform, creating and releasing 20 threads.

Under .NET:

The SetMinThreads() seems to initialise a collection of threads at this size.  These can be used without delay.  After this limit it reached, extra threads are created with a delay of about .5 seconds.
When released, the thread pool under .NET seems to shrink back down to the value of the SetMaxThreads(). 

Therefore the test is repeatable after a release of all threads: threads under this limit come from the pool, over this limit are created with a delay.

Under Mono:

It seems that both the SetMinThreads() and SetMaxThreads() are ignored.  Each thread is created with a delay.  On release of threads, the thread pool seems to maintain its maximum size, not the size set in SetMaxThreads().  Therefore a second allocation of threads will allocate without delay.
Comment 2 Andres G. Aragoneses 2013-07-31 18:35:17 UTC
Is https://github.com/mono/mono/pull/640 a fix or partial fix for this?
Comment 3 Rodrigo Kumpera 2017-07-13 00:01:29 UTC
This now works as expected.