tags:

views:

838

answers:

3

Is there a magic number or formula for setting the values of SetMaxThreads and SetMinThreads for ThreadPool? I have thousands of long-running methods that need execution but just can't find the perfect match for setting these values. Any advise would be greatly appreciated.

+4  A: 

Typically, the magic number is to leave it alone. The ThreadPool does a good job of handling this.

That being said, if you're doing a lot of long running services, and those services will have large periods where they're waiting, you may want to increase the maximum threads to handle more options. (If the processes aren't blocking, you'll probably just slow things down if you increase the thread count...)

Profile your application to find the correct number.

Reed Copsey
If I were to change the max threads (and min threads?) there are two parameters, worker threads and completion port threads. What's the completion port thread count for??
Benny
Also, if I leave the ThreadPool alone, without setting min/max threads I run into an OutOfMemoryException and all threads fail.
Benny
That's a different issue - you can reduce the amount of work by throttling it, but running out of memory really has nothing to do with threading...
Reed Copsey
+2  A: 

If you want better control, you might want to consider NOT using the built-in ThreadPool. There is a nice replacement at http://www.codeproject.com/KB/threads/smartthreadpool.aspx.

Michael Bray
I've attempted to use SmartThreadPool, but to no avail. After I queued all of the work items, I attempted to use WaitForIdle (I have post-execution logic to run) and it never blocks, just moves right on. Might be between the keyboard and the computer, but haven't figured that one out yet
Benny
+4  A: 

The default minimum number of threads is the number of cores your machine has. That's a good number, it doesn't make sense to run more threads than you have cores.

The default maximum number of threads is 250 times the number of cores you have. There is an enormous amount of breathing room here. On a four core machine, it would take 499 seconds to reach that maximum if none of the threads complete in a reasonable amount of time.

The threadpool scheduler tries to limit the number of active threads to the number of cores you have. Twice a second it allows one more thread to start if the active threads do not complete. Threads that run for a very long time or do a lot of blocking that is not caused by I/O are not good candidates for the threadpool. You should use a regular Thread instead.

Getting to the maximum isn't healthy. On a four core machine, just the stacks of those threads will consume a gigabyte of virtual memory space. Getting OOM is very likely. Consider lowering the max number of threads if that's your problem. Or consider starting just a few regular Threads that receive packets of work from a thread-safe queue.

Hans Passant