[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]
The Task Parallel Library (TPL) is a set of public types and APIs in the System.Threading and System.Threading.Tasks namespaces in the .NET Framework version 4. These types rely on a task scheduler that is integrated with the .NET ThreadPool. The purpose of the TPL is to make developers more productive by simplifying the process of adding parallelism and concurrency to applications.
The TPL scales the degree of concurrency dynamically to most efficiently use all the processors that are available. Parallel code that is based on the TPL not only works on dual-core and quad-core computers. It will also automatically scale, without recompilation, to manycore computers.
When you use the TPL, writing a multithreaded for loop closely resembles writing a sequential for loop. The following code automatically partitions the work into tasks, based on the number of processors on the computer.
Parallel.For(startIndex, endIndex, Sub(currentIndex) DoSomeWork(currentIndex))
Parallel.For(startIndex, endIndex, (currentIndex) => DoSomeWork(currentIndex));
By using ParallelInvoke()()(), you can invoke any number of asynchronous, concurrent operations by using just one method call, as follows.
Parallel.Invoke(
Sub() MethodA(),
Sub() MethodB(),
Sub() MethodC())
Parallel.Invoke(
() => MethodA(),
() => MethodB(),
() => MethodC());
By using TPL to parallelize your code, you can focus on the work that your program is designed to accomplish. However, not all sequential loops are good candidates for parallelization. Parallel loops, like any multithreaded code, can introduce complexity into your program execution. Although the TPL simplifies multithreaded scenarios, we recommend that you have a basic understanding of threading concepts, for example, locks, deadlocks, and race conditions, so that you can use the TPL effectively.
In the TPL, the basic abstraction is the Task, not the thread. A task is an instance of the Task class. A task can be canceled and waited on, can return a value, and can invoke another task when it completes. When you use Parallel.For and Parallel.ForEach, even the Task object itself is implicit. In your code, you just supply the delegate that performs the desired work. The TPL handles the rest. By default, the TPL uses its own task scheduler, which is integrated with the .NET ThreadPool. However, you can also provide a custom task scheduler that uses some other thread-scheduling mechanism. There is no fixed relationship between a task and a thread. A thread may run several tasks in succession for any given block of parallel code. A task may define a child task that runs on the same thread, or a different thread. A task may also invoke another task that has been defined elsewhere.
You can also access parallel functionality in declarative syntax by using Parallel LINQ (PLINQ). Internally, PLINQ is closely integrated with the Task Parallel Library. For more information, see Parallel LINQ (PLINQ).