Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it true that for long running processes it is better to do thread manually instead of threadpool?

I read on the other day that for long-running tasks my best bet is to manually create threads instead of using .NET’s thread pool or Task Parallel. I'd really like someone to enlighten me as I am learning about c# threading, specially for long running IO tasks. Thank you in advance.

like image 749
user1193665 Avatar asked Apr 24 '12 13:04

user1193665


People also ask

What is the difference between thread and ThreadPool?

A thread pool is - as the name suggests - a pool of worker threads which are always running. Those threads then normally take tasks from a list, execute them, then try to take the next task. If there's no task, the thread will wait.

What is a ThreadPool and is it more effective than using several separate threads?

A thread pool reuses previously created threads to execute current tasks and offers a solution to the problem of thread cycle overhead and resource thrashing. Since the thread is already existing when the request arrives, the delay introduced by thread creation is eliminated, making the application more responsive.

Why is ThreadPool needed?

The thread pool creates and destroys worker threads in order to optimize throughput, which is defined as the number of tasks that complete per unit of time. Too few threads might not make optimal use of available resources, whereas too many threads could increase resource contention. You can use the ThreadPool.

When should you not use thread pool?

Thread pools do not make sense when you need thread which perform entirely dissimilar and unrelated actions, which cannot be considered "jobs"; e.g., One thread for GUI event handling, another for backend processing. Thread pools also don't make sense when processing forms a pipeline.


1 Answers

That is true. The thread pool is optimised for small units of work and you can interfere with other work by holding onto a thread pool thread.

My rule of thumb is if an operation can take more than a second, it should not be on a thread pool thread. That is probably quite long.

Although this is undocumented, if you start a Task with TaskCreationOptions.LongRunning then a new Thread will be started to run the Task.

For most IO tasks, there are asynchronous versions of the framework methods that you should really use. These make use of kernel functions and mean that you won't be blocking any thread.

As always, I recommend reading Joe Albahari's free ebook, followed by Joe Duffy's Concurrent Programming on Windows. The later is 1000 pages long, but is full of useful details.

like image 194
Nick Butler Avatar answered Nov 15 '22 03:11

Nick Butler