ThreadPool Question

B

Bill

I want to use the threadpool to perform multiple tasks at once. The problem is that I was trying to find a way to only have say X number of threads running at once instead of the using the entire thread pool. I read this in MSDN,

"..there's only one pool per process and we cannot create a new one. The purpose of this limitation is to centralize all the asynchronous programming in the same pool, so that we do not have a third-party component that creates a parallel pool that we cannot manage and whose threads are degrading our performance."


But what if say I want to use the Threadpool.QueueUserWorkItem(), and have about 500 items to queue but I only want 2 threads working on the queue at once. How do I accomplish this?
 
D

drew

I want to use the threadpool to perform multiple tasks at once. The problem is that I was trying to find a way to only have say X number of threads running at once instead of the using the entire thread pool. I read this in MSDN,

"..there's only one pool per process and we cannot create a new one. The purpose of this limitation is to centralize all the asynchronous programming in the same pool, so that we do not have a third-party component that creates a parallel pool that we cannot manage and whose threads are degrading our performance."


But what if say I want to use the Threadpool.QueueUserWorkItem(), and have about 500 items to queue but I only want 2 threads working on the queue at once. How do I accomplish this?
 
D

drew

Well you can either reduce the thread pool count in the machine.config but that would lower it for everything other application as well.
I want to use the threadpool to perform multiple tasks at once. The problem is that I was trying to find a way to only have say X number of threads running at once instead of the using the entire thread pool. I read this in MSDN,

"..there's only one pool per process and we cannot create a new one. The purpose of this limitation is to centralize all the asynchronous programming in the same pool, so that we do not have a third-party component that creates a parallel pool that we cannot manage and whose threads are degrading our performance."


But what if say I want to use the Threadpool.QueueUserWorkItem(), and have about 500 items to queue but I only want 2 threads working on the queue at once. How do I accomplish this?
 
B

Bill

Would this be a logical solution? Keep a running count of items queued and just wait until the worker thread finishes before I queue the next item? It seems like there should be a better way to do this.

myMAXThreadCount = 5;
for(int x=0; x<500; x++)
{
Interlocked.Increment(ref numThreadsStarted);
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc),this);
while(numThreadsStarted==myMAXThreadCount)
{
Thread.Sleep(500);
}
}

internal static void ThreadProc(Object stateInfo)
{
...Do work...
Interlocked.Decrement(ref stateInfo.numThreadsStarted);
}

Well you can either reduce the thread pool count in the machine.config but that would lower it for everything other application as well.
I want to use the threadpool to perform multiple tasks at once. The problem is that I was trying to find a way to only have say X number of threads running at once instead of the using the entire thread pool. I read this in MSDN,

"..there's only one pool per process and we cannot create a new one. The purpose of this limitation is to centralize all the asynchronous programming in the same pool, so that we do not have a third-party component that creates a parallel pool that we cannot manage and whose threads are degrading our performance."


But what if say I want to use the Threadpool.QueueUserWorkItem(), and have about 500 items to queue but I only want 2 threads working on the queue at once. How do I accomplish this?
 
W

Willy Denoyette [MVP]

No, the config files threapool count is used by asp.net only.

Willy.

Well you can either reduce the thread pool count in the machine.config but that would lower it for everything other application as well.
I want to use the threadpool to perform multiple tasks at once. The problem is that I was trying to find a way to only have say X number of threads running at once instead of the using the entire thread pool. I read this in MSDN,

"..there's only one pool per process and we cannot create a new one. The purpose of this limitation is to centralize all the asynchronous programming in the same pool, so that we do not have a third-party component that creates a parallel pool that we cannot manage and whose threads are degrading our performance."


But what if say I want to use the Threadpool.QueueUserWorkItem(), and have about 500 items to queue but I only want 2 threads working on the queue at once. How do I accomplish this?
 
D

David Browne

Would this be a logical solution? Keep a running count of items queued and
just >wait until the worker thread finishes before I queue the next item?
It seems like >there should be a better way to do this.
myMAXThreadCount = 5;
for(int x=0; x<500; x++)
{
Interlocked.Increment(ref numThreadsStarted);
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc),this);
while(numThreadsStarted==myMAXThreadCount)
{
Thread.Sleep(500);
}
}
internal static void ThreadProc(Object stateInfo)
{
...Do work...
Interlocked.Decrement(ref stateInfo.numThreadsStarted);
}

That will work fine. You don't want to change the threads in the thread
pool, or even investigate how many are currently waiting there since your
appDomain does not own the thread pool.

David
 
J

Jon Skeet [C# MVP]

Bill said:
I want to use the threadpool to perform multiple tasks at once. The
problem is that I was trying to find a way to only have say X number
of threads running at once instead of the using the entire thread
pool. I read this in MSDN,

"..there's only one pool per process and we cannot create a new one.
The purpose of this limitation is to centralize all the asynchronous
programming in the same pool, so that we do not have a third-party
component that creates a parallel pool that we cannot manage and
whose threads are degrading our performance."


But what if say I want to use the Threadpool.QueueUserWorkItem(), and
have about 500 items to queue but I only want 2 threads working on
the queue at once. How do I accomplish this?

This is the problem with there being one system-wide ThreadPool. It
would be lovely if an application could start its own ThreadPool (or
multiple ones) with specific parameters such as thread starting policy,
thread death policy, number of threads etc.

A while ago I considered writing one, but there are lots of
complications in terms of security, impersonation etc. I suppose that
it might still be worth writing one for people for whom those problems
aren't important... let me know if you're interested.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top