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

13,654

Solution 1

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.

Solution 2

You are right, ThreadPool thread is lightweight and cheap since could be rescheduled to serve a new request from the ThreadPool, so as soon as thread operation is done, ThreadPool could reschedule the same thread for other operation, also you can manipulate by minimum threads count (ThreadPool.SetMinThreads()), so those would be alive until new requests are come. So this is a good solution for multiple lightweight operations, for instance you need to create a separate/new thread each few seconds.

Very good article on MSDN Magazine: Dedicated thread or a Threadpool thread?

Once the minimum number of threads is reached, the thread pool aims to limit the number of threads being created to one per 500 milliseconds. This is an intelligent mechanism, avoiding the expensive cost of creating a new thread when multiple thread pool threads may be released within that time period.

Since .NET 4.0 - Task Parallel Library is a good high-level abstraction and alternative for the manual thread management and synchronization, so your code would be less error prone. So just create a Task with TaskCreationOptions.LongRunning, I believe this would be the best investment in an application architecture from the maintability perspectives.

Useful reading:

Share:
13,654
user1193665
Author by

user1193665

Updated on June 05, 2022

Comments

  • user1193665
    user1193665 almost 2 years

    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.

  • user1193665
    user1193665 almost 12 years
    Thank you all very much for your directions.
  • supercat
    supercat almost 11 years
    The criterion I would use would be whether the thread is expected to spend any significant time "waiting" for something to happen, or performs some particular action that another thread might be waiting for.
  • TorbenJ
    TorbenJ over 4 years
    Although this is undocumented, if you start a Task with TaskCreationOptions.LongRunning then a new Thread will be started to run the Task. Microsoft Docs added this in the mean time apparently docs.microsoft.com/en-us/dotnet/api/… [...] It also provides a hint to the task scheduler that an additional thread might be required for the task [...]