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:

ScreenShot024

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.

No comments:

Post a Comment