Psst! Did you know DevelopmentNow is a mobile web site design agency?

Contact us for help mobilizing your site, or to sign up for our beta Mobile Web SDK!
all groups > dotnet framework > april 2008 >

dotnet framework : ThreadPool.SetMaxThreads


Michael D. Ober
4/19/2008 12:48:27 PM
Does the ThreadPool in framework 3.5 honor the SetMaxThreads method? The
reason I ask is that I started a program that queued 256 worker threads in
the threadpool and after a few minutes, task manager (and the console
output) showed all of them working. I have situations where I need to limit
the number of active tasks because they are resource intensive on a
different system but not on the local workstation and I need to ensure I
don't kill the other system's responsiveness.

Thanks,
Mike.

Jeroen Mostert
4/19/2008 9:15:03 PM
[quoted text, click to view]

You can't queue worker threads, you queue work items. The thread pool then
decides to allocate threads for those items as it sees fit.

[quoted text, click to view]

You mean to say that task manager showed that the process had 256+ threads
running? That would be surprising to me. I ran this little test program:

static void Main(string[] args) {
ThreadPool.SetMaxThreads(20, 20);
int i = 0;
while (true) {
ThreadPool.QueueUserWorkItem(
delegate(object o) {
while (true) {
Console.WriteLine("Work item {0} reporting in", o);
Thread.Sleep(1000);
}
},
++i
);
Thread.Sleep(100);
}
}

Basically this is the worst way to use the thread pool; I'm queueing up
items forever that additionally never complete. Process Explorer shows the
number of threads stabilizing at 27 and indeed the output never progresses
beyond "worker item 20 reporting in"; the additional work items are simply
queued up but never executed.

If I remove the loop in the thread delegate (ensuring that worker items
finish quickly) the number of threads never goes above 9 and no additional
worker threads are allocated.

[quoted text, click to view]
Well, to begin with, the thread pool is not a limiting mechanism on
concurrency nor is it supposed to be used for work items that take a lot of
time to complete. It's intended to utilize the system's potential for
concurrency as much as possible. If you need to limit the maximum number of
outstanding tasks, you should do so yourself (with a semaphore, for example).

--
J.
Dan Kelley
4/21/2008 1:21:00 AM
Michael kennedy blogged about changes to the ThreadPool in 3.5 - check out
the link
http://www.michaelckennedy.net/blog/PermaLink,guid,55a9b21e-ae85-4c24-a0b6-63dff4a6b491.aspx.

[quoted text, click to view]
Michael D. Ober
4/21/2008 5:55:06 AM
Dan,

This matches what I saw in my program. I submitted 256 jobs to the
threadpool and before they were all done, I had 267 threads. Some of those
threads were obviously the runtime overhead and one was the master thread
that did all the submits.

So, back to my original question - will SetMaxThreads be honored? If I get
a chance, I'll try it today.

Thanks,
Mike.


[quoted text, click to view]


Michael D. Ober
4/21/2008 10:43:15 PM
[quoted text, click to view]

I did some testing today and the SetMaxThreads is indeed honored in 3.5. I
was also able to duplicate the general shape of the graphs in Michael
Kennedy's blog as well. I didn't test for the reported SetMinThreads bug as
the code I have that would trigger that bug already has a half second sleep
built in as the server side can't keep up otherwise.

Mike.

[quoted text, click to view]

AddThis Social Bookmark Button