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.

Re: ThreadPool.SetMaxThreads by Jeroen

Jeroen
Sat Apr 19 14:15:03 CDT 2008

Michael D. Ober wrote:
> 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

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.

> and after a few minutes, task manager (and the
> console output) showed all of them working.

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.

> 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.
>
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.
http://symbolsprose.blogspot.com

RE: ThreadPool.SetMaxThreads by DanKelley

DanKelley
Mon Apr 21 03:21:00 CDT 2008

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.

"Michael D. Ober" wrote:

> 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.
>
>
>

Re: ThreadPool.SetMaxThreads by Michael

Michael
Mon Apr 21 06:55:08 CDT 2008

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.


"Dan Kelley" <DanKelley@discussions.microsoft.com> wrote in message
news:510D874F-55D2-4D0B-B771-BF62CB3ED350@microsoft.com...
> 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.
>
> "Michael D. Ober" wrote:
>
>> 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.
>>
>>
>>
>




Re: ThreadPool.SetMaxThreads by Michael

Michael
Mon Apr 21 23:43:18 CDT 2008

"Michael D. Ober" <obermd.@.alum.mit.edu.nospam.> wrote in message
news:zbydnQNQTqQBHZHVnZ2dnUVZ_ommnZ2d@earthlink.com...
> 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.
>

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.

>
> "Dan Kelley" <DanKelley@discussions.microsoft.com> wrote in message
> news:510D874F-55D2-4D0B-B771-BF62CB3ED350@microsoft.com...
>> 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.
>>
>> "Michael D. Ober" wrote:
>>
>>> 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.