threadpool queue control

  • Thread starter Thread starter Keith O
  • Start date Start date
K

Keith O

I know that you can queue thousands of jobs to the threadpool, but what if I
want to control how many jobs are queued. Here is an algorithm I came up
with. Please tell me if there is anything wrong with it, or if you have a
better way to handle this:


int c = 0;

while (DB.readRow())
{
if (c < MAX_QUEUE_SIZE)
{
c++;
ThreadPool.QueueUserWorkItem(new WaitCallback(DoThis));
}
else
{
lock.Wait();
}
}

....
....
....

public void DoThis()
{
...
...
...
c--;
lock.Pulse();
}
 
Keith O said:
I know that you can queue thousands of jobs to the threadpool, but what if I
want to control how many jobs are queued. Here is an algorithm I came up
with. Please tell me if there is anything wrong with it, or if you have a
better way to handle this:

Yup, there's a better way to handle it - use your own thread pool.
Using the system thread pool has distinct disadvantages:

1) If your code does things which use the thread pool - and you may not
be aware that they're doing so - you can end up with deadlock.

2) You can't control how many threads there are, or how quickly they
die, etc

See http://www.pobox.com/~skeet/csharp/miscutil for a custom thread
pool you can use in your app.
 
Back
Top