ThreadPool - why no SetMaxThreads?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi...

I know this will seem like a picayune question, but why is there a
SetMinThreads but no SetMaxThreads on the ThreadPool class?

Thanks
_mark
 
SetMinThreads allows you to "warm up" the thread pool. The threadpool heuristic will pause for 500ms before spawning a new thread in general. If you know you are going to have to do 5 things concurrently it may be better to get the threadpool to spawn these threads before you start assigning work. Setting the maximum number of threads has the *potential* to allow people to flood the system with threads so it was omitted from 1.1. However it has been added to the threadpool class for version 2.0

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hi...

I know this will seem like a picayune question, but why is there a
SetMinThreads but no SetMaxThreads on the ThreadPool class?

Thanks
_mark
 
Thanks for the great answer. I'm not sure how the reasoning follows there at
the end, though. If you have SetMinThreads and that "warms up" the pool by
preallocating the threads, doesn't *that* give the developer the potential of
flooding the system?

Maybe I'm thinking about it backwards, but I was thinking that SetMaxThreads
would essentially be giving the developer the ability to *stop* flooding the
system by putting an upper bound. If I queue, say, 100 tasks and I don't
want the pool to take a quiet moment on the system to launch threads for all
of them - if I want at most 10 active threads or so - it seems like that
would be a stopper on flooding the system rather than encouraging it...

Thanks
-mark
 
Mark said:
Thanks for the great answer. I'm not sure how the reasoning follows there
at
the end, though. If you have SetMinThreads and that "warms up" the pool
by
preallocating the threads, doesn't *that* give the developer the potential
of
flooding the system?
No because the number of threads you can set with SetMinThreads is bound to
the maximum number of threads in the pool...

From framework doc:
"If you specify a negative number or a number larger than the maximum number
of active thread pool threads (obtained using GetMaxThreads), SetMinThreads
returns false and does not change either of the minimum values."
 
Mark said:
Thanks for the great answer. I'm not sure how the reasoning follows there at
the end, though. If you have SetMinThreads and that "warms up" the pool by
preallocating the threads, doesn't *that* give the developer the potential of
flooding the system?

Maybe I'm thinking about it backwards, but I was thinking that SetMaxThreads
would essentially be giving the developer the ability to *stop* flooding the
system by putting an upper bound. If I queue, say, 100 tasks and I don't
want the pool to take a quiet moment on the system to launch threads for all
of them - if I want at most 10 active threads or so - it seems like that
would be a stopper on flooding the system rather than encouraging it...

There are an awful lot of restrictions to using the system threadpool.
As lots of framework classes use it - often unexpectedly, IMO - it's
quite easy to get yourself into a potential deadlock situation.

If you're doing anything significant with it, I prefer to use a custom
threadpool which is entirely separate and can be tweaked appropriately.

There are quite a few available, and I have one in my utility library:
http://www.pobox.com/~skeet/csharp/miscutil
 
The thread pool is already capped. In v1.1 you cannot change that max value from managed code (although you can by using interop). The max is set at 25 threads per processor. v2.0 gives a managed interface to changing the maximum size of the threadpool.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Thanks for the great answer. I'm not sure how the reasoning follows there at
the end, though. If you have SetMinThreads and that "warms up" the pool by
preallocating the threads, doesn't *that* give the developer the potential of
flooding the system?

Maybe I'm thinking about it backwards, but I was thinking that SetMaxThreads
would essentially be giving the developer the ability to *stop* flooding the
system by putting an upper bound. If I queue, say, 100 tasks and I don't
want the pool to take a quiet moment on the system to launch threads for all
of them - if I want at most 10 active threads or so - it seems like that
would be a stopper on flooding the system rather than encouraging it...

Thanks
-mark
 
Thanks for all the responses. In my last, I just meant that SetMaxThreads
(if and when it exists) could also be capped at a theoretical max. The
documentation says that the max is some calculation on the number of
processes, available memory, cpu utilization, etc. If the max is 25,
SetMaxThreads=8, for example, would be a way for the programmer to
intentionally throttle back how much of the available pie he wanted his
program to take.

SetMinThreads, conceptually, seems less of a throttle, since it's going to
make sure x threads are created even if you only queue a smaller number of
items. That's all I was saying.

Will SetMaxThreads in 2.0 also be imposing a maximum cap or will it be wide
open?

Thanks
-mark
 
The threadpool is a process specific construct and SetMaxThreads in v2.0 specifies the maximum number of thread pool threads for that process

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Thanks for all the responses. In my last, I just meant that SetMaxThreads
(if and when it exists) could also be capped at a theoretical max. The
documentation says that the max is some calculation on the number of
processes, available memory, cpu utilization, etc. If the max is 25,
SetMaxThreads=8, for example, would be a way for the programmer to
intentionally throttle back how much of the available pie he wanted his
program to take.

SetMinThreads, conceptually, seems less of a throttle, since it's going to
make sure x threads are created even if you only queue a smaller number of
items. That's all I was saying.

Will SetMaxThreads in 2.0 also be imposing a maximum cap or will it be wide
open?

Thanks
-mark
 
Back
Top