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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With