vote buttons

What is the difference between tasks (in TPL) and threads.

What are the advantages and disadvantages of using either one.

When should we use one instead of the other.

11-Oct-2014 04:15 UTC
K Bonneau

1 Answers

vote buttons
This is actually not a valid comparison. But a question can be asked about advantages of Tasks v/s traditional threading mechanisms.

An important difference is in the thread pool each mechanism uses. See this thread which states the differences between normal thread pool and TPL thread pool which gets used when using tasks. Other differences:

  • There’s no easy way to get a "return value" back from a thread that is joined without having to set up some kind of shared field. And if the operation throws an exception, catching and propagating that exception is equally painful. With Tasks we can get the return value easily and it also makes dealing with exceptions easy.
  • You can’t tell a thread to start something else when it’s finished. You have to join it there by blocking the thread calling it in the process. With tasks this becomes easy as you can tell a task to invoke other task when its finished.
  • Tasks are compositional i.e you can chain them together through the use of continuations.
  • TPL is designed for fine grained concurrency

11-Oct-2014 04:50 UTC
Super Human
Related Questions