Thursday, 19 May 2011

Tasks vs Threading

With the introduction of the Task Parallel Library (TPL) within .NET 4.0, creating a Task rather than a Thread is now the preferred way to create multithreaded applications.

Here is an example showing the creation of a thread using the old style Threading vs the new Tasks:


Why chose Task over Threading?

Behind the scenes, tasks are queued to the ThreadPool, which has been enhanced with algorithms (like hill-climbing) that determine and adjust to the number of threads that maximizes throughput. This makes tasks relatively lightweight, and you can create many of them to enable fine-grained parallelism. To complement this, widely-known work-stealing algorithms are employed to provide load-balancing.


  1. Is there such a thing as "task safety"? I appears many of the locking and synchronization mechanism used with threads would not work properly with tasks (for example, operations that block the thread on some event would hang the program if all tasks happen to be implemented on a single thread. Is there a source you know of that deals with issues like critical code segments? Thanks!