views:

75

answers:

3

Is there any way (apart form actual perfomance measurements which can be pretty hard to make them realistic) or rule of thumb when I should stop using the ThreadPool and use a dedicated Thread instead? I suppose for long running work it is better to use a dedicated Thread because it doesn't peramently steal one from the ThreadPool. For shorter work it is better use the the ThreadPool because creating threads and the thread itself consumes a lot of resources.

But where is the magic barrier? How do I decide which approach to use?

In simple applications it might not matter that much. But I am dealing with a custom .NET-based application framework where a single application can have a lot of plugins which in most cases need to do some work asynchonously. And I am thinking about introducing a policy for the plugin authors when to use which.

+1  A: 

If your application is going to use alot of threads you can increase the minimum number of threads in the ThreadPool to avoid a bottleneck there. I don't see any advantages of dedicated threads. The only thing that comes to mind is that you can abort a dedicated thread (which is not advisable anyway).

Performance should be the same. The task parallel library in the .NET 4.0 framework only uses the ThreadPool so it shouldn't be a problem for your project either.

testalino
You can't control exactly when a pooled thread will execute. I would also strongly recommend against increasing the threadpool size; if you need to do that, it is very likely you need to rethink the whole threading system you are using. There are other things you can do with the Thread class but not pooled threads; like get/set priority, foreground/background...
Andrew Barber
Also, you should probably never use a ThreadPool thread if the thread could end up being blocked waiting for some sort of IO operation.
Andrew Barber
Is there a way to dynamically determine if it is wise to increase the number of threadpool threads? Suppose I have 20+ plugins that each keep blocking a ThreadPool thread, how can I detect that and add some more threads to the ThreadPool? Is that approach a good idea afer all?
bitbonk
It is correct that you can't control when the thread starts exactly, but it is not like there would be any "unnecessary" waits. I also missed the additional properties like priority because I never had to use them (and most applications shouldn't either).
testalino
@bitbonk, I don't think it is always a bad thing to increase the number of threads. Thats why this method exists after all. You can call ThreadPool.GetAvailableThreads to see if there are enough free threads available.
testalino
@bitbonk, you don't have to increase the (min) number of threads. It will grow automatically and although the algorithm is far from perfect it is hard for you to improve on it.
Henk Holterman
+3  A: 

Use thread pool threads for short running tasks.

Use threads if you need a long running task or need to set some properties of the thread such as priority, foreground, culture etc.

If possible use the Task class in .NET 4. The default scheduler uses the thread pool, but you may provide your own scheduler and thus control exactly how tasks are mapped to threads.

Brian Rasmussen
But where is the magic barrier? How do I decide which approach to use? **What is short and what is long?**
bitbonk
@bitbonk: I'm not sure you can identify a specific metric for this that works in all cases, but you generally want to avoid large loops, operations that may block and so forth.
Brian Rasmussen
@bitbonk: Also, even if you could come up with a specific number, this would be tied to an implementation detail, which may change in the future. Some tasks are obviously short running and others are obviously long running. For the rest of them, you may need to try out both approaches to find the optimal solution.
Brian Rasmussen
+2  A: 

It is quantifiable. On my dual core laptop, the threadpool scheduler releases an extra tp thread twice a second if the running ones are not making progress. So "long" is more than half a second.

I do not know how this scales with beefier hardware. It is easy to test, just start 16 tp threads and have them write DateTime.Now and Sleep(8001).

Also, any thread that is very likely to block for extended periods, either on a lock or slow I/O should be a regular thread. Because no useful work will be done by the processor, the blocking thread is preventing other tp threads that might have useful work to do from running for at least that half second. Of course, that makes such a thread almost automatically qualify as a "long" thread.

Hans Passant