Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread.Start() versus ThreadPool.QueueUserWorkItem()

The Microsoft .NET Base Class Library provides several ways to create a thread and start it. Basically the invocation is very similar to every other one providing the same kind of service: create an object representing an execution flow (or more), assign it a delegate representing the execution flow to execute and, eventually, depending on delegate signature, an object as a parameter.

Well, there are two approaches (essentially):

1) Using the System.Threading.Thread class.

Thread curr = new Thread(myfunction); /* In a class, myfunction is a void taking an object */ curr.Start(new Object()); /* Or something else to be downcast */ 

2) Using the System.Threading.ThreadPool class.

ThreadPool.QueueUserWorkItem(myfunction, new Object()); /* Same philosophy here */ 

Are there any special reasons why I should use 1) or 2)?

  • Performance reasons?
  • Patterns?
  • What is the best approach?

I have a feeling that the answer is: "Depend by the situation". Could you please list some situations where one approach is better than another?

like image 999
Andry Avatar asked May 31 '11 20:05

Andry


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 ThreadPool QueueUserWorkItem?

QueueUserWorkItem(WaitCallback, Object) Queues a method for execution, and specifies an object containing data to be used by the method. The method executes when a thread pool thread becomes available.

When should you not use 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.

Why is ThreadPool needed?

If you have short tasks that require background processing, the managed thread pool is an easy way to take advantage of multiple threads. Use of the thread pool is significantly easier in Framework 4 and later, since you can create Task and Task<TResult> objects that perform asynchronous tasks on thread pool threads. .


2 Answers

Starting a new thread can be a very expensive operation. The thread pool reuses threads and thus amortizes the cost. Unless you need a dedicated thread, the thread pool is the recommended way to go. By using a dedicated thread you have more control over thread specific attributes such as priority, culture and so forth. Also, you should not do long running tasks on the thread pool as it will force the pool to spawn additional threads.

In addition to the options you mention .NET 4 offers some great abstractions for concurrency. Check out the Task and Parallel classes as well as all the new PLINQ methods.

like image 51
Brian Rasmussen Avatar answered Sep 21 '22 00:09

Brian Rasmussen


The Managed Thread Pool has some very good guidelines on when NOT to use the thread pool.

In my experience, you want to create your own thread when you need a persistent, dedicated, long-running thread. For everything else, use asynchronous delegates or something like QueueUserWorkItem, BackgroundWorker, or the Task-related features of .NET 4.0.

like image 29
Jim Mischel Avatar answered Sep 20 '22 00:09

Jim Mischel