数据并行(任务并行库)Data Parallelism (Task Parallel Library)

数据并行 指的是对源集合或数组的元素同时(即,并行)执行相同操作的场景。Data parallelism refers to scenarios in which the same operation is performed concurrently (that is, in parallel) on elements in a source collection or array. 在数据并行操作中,对源集合进行分区,以便多个线程能够同时在不同的网段上操作。In data parallel operations, the source collection is partitioned so that multiple threads can operate on different segments concurrently.

任务并行库 (TPL) 支持通过 System.Threading.Tasks.Parallel 类实现的数据并行。The Task Parallel Library (TPL) supports data parallelism through the System.Threading.Tasks.Parallel class. 此类对 for 循环和 foreach 循环(Visual Basic 中的 ForFor Each)提供了基于方法的并行执行。This class provides method-based parallel implementations of for and foreach loops (For and For Each in Visual Basic). 你为 Parallel.ForParallel.ForEach 循环编写的循环逻辑与编写连续循环的相似。You write the loop logic for a Parallel.For or Parallel.ForEach loop much as you would write a sequential loop. 无需创建线程或列工作项。You do not have to create threads or queue work items. 在基本循环中,不需要加锁。In basic loops, you do not have to take locks. TPL 为你处理所有低级别的工作。The TPL handles all the low-level work for you. 有关使用 Parallel.ForParallel.ForEach 的详细信息,请下载文档并行编程模式:了解并应用与 .NET Framework 4 的并行模式For in-depth information about the use of Parallel.For and Parallel.ForEach, download the document Patterns for Parallel Programming: Understanding and Applying Parallel Patterns with the .NET Framework 4. 下面的代码示例演示了一个简单的 foreach 循环及其并行等效项。The following code example shows a simple foreach loop and its parallel equivalent.

备注

本文档使用 lambda 表达式在 TPL 中定义委托。This documentation uses lambda expressions to define delegates in TPL. 如果不熟悉 C# 或 Visual Basic 中的 lambda 表达式,请参阅 PLINQ 和 TPL 中的 Lambda 表达式If you are not familiar with lambda expressions in C# or Visual Basic, see Lambda Expressions in PLINQ and TPL.

// Sequential version
foreach (var item in sourceCollection)
{
    Process(item);
}

// Parallel equivalent
Parallel.ForEach(sourceCollection, item => Process(item));
' Sequential version        
For Each item In sourceCollection
    Process(item)
Next

' Parallel equivalent
Parallel.ForEach(sourceCollection, Sub(item) Process(item))

并行循环运行时,TPL 将数据源进行分区,以便该循环可以同时对多个部分进行作用。When a parallel loop runs, the TPL partitions the data source so that the loop can operate on multiple parts concurrently. 在后台,任务计划程序基于系统资源和工作负荷来划分任务。Behind the scenes, the Task Scheduler partitions the task based on system resources and workload. 如有可能,如果工作负荷变得不平衡了,计划程序将重新分配多个线程与处理器之间的工作。When possible, the scheduler redistributes work among multiple threads and processors if the workload becomes unbalanced.

备注

你也可以提供你自己的自定义分区程序或计划程序。You can also supply your own custom partitioner or scheduler. 有关详细信息,请参阅 PLINQ 和 TPL 的自定义分区程序任务计划程序For more information, see Custom Partitioners for PLINQ and TPL and Task Schedulers.

Parallel.ForParallel.ForEach 方法都有多个过载,可让你停止或中断循环执行,监视其它线程上循环的状态,保持本地线程状态,完成本地线程对象,控制并发程度等等。Both the Parallel.For and Parallel.ForEach methods have several overloads that let you stop or break loop execution, monitor the state of the loop on other threads, maintain thread-local state, finalize thread-local objects, control the degree of concurrency, and so on. 启用此功能的帮助器类型包括 ParallelLoopStateParallelOptionsParallelLoopResultCancellationTokenCancellationTokenSourceThe helper types that enable this functionality include ParallelLoopState, ParallelOptions, ParallelLoopResult, CancellationToken, and CancellationTokenSource.

有关详细信息,请参阅并行编程模式:了解并应用与 .NET Framework 4 的并行模式For more information, see Patterns for Parallel Programming: Understanding and Applying Parallel Patterns with the .NET Framework 4.

PLINQ 支持使用声明性或查询类语法的数据并行。Data parallelism with declarative, or query-like, syntax is supported by PLINQ. 有关详细信息,请参阅并行 LINQ (PLINQ)For more information, see Parallel LINQ (PLINQ).

TitleTitle 描述Description
如何:编写简单的 Parallel.For 循环How to: Write a Simple Parallel.For Loop 描述如何编写遍历任何数组或可变址 IEnumerable<T> 源集合的 For 循环。Describes how to write a For loop over any array or indexable IEnumerable<T> source collection.
如何:编写简单的 Parallel.ForEach 循环How to: Write a Simple Parallel.ForEach Loop 描述如何编写遍历任何 IEnumerable<T> 源集合的 ForEach 循环。Describes how to write a ForEach loop over any IEnumerable<T> source collection.
如何:从 Parallel.For 循环停止或中断How to: Stop or Break from a Parallel.For Loop 描述如何停止或中断并行循环,以便所有线程都获得该操作的通知。Describes how to stop or break from a parallel loop so that all threads are informed of the action.
如何:编写具有线程局部变量的 Parallel.For 循环How to: Write a Parallel.For Loop with Thread-Local Variables 描述如何编写 For 循环,该循环中每个线程都维持有对其它任何线程不可见的私有变量,以及如何在循环完成时,同步所有线程的结果。Describes how to write a For loop in which each thread maintains a private variable that is not visible to any other threads, and how to synchronize the results from all threads when the loop completes.
如何:使用分区本地变量编写 Parallel.ForEach 循环How to: Write a Parallel.ForEach Loop with Partition-Local Variables 描述如何编写 ForEach 循环,该循环中每个线程都维持有对其它任何线程不可见的私有变量,以及如何在循环完成时,同步所有线程的结果。Describes how to write a ForEach loop in which each thread maintains a private variable that is not visible to any other threads, and how to synchronize the results from all threads when the loop completes.
如何:取消 Parallel.For 或 ForEach 循环How to: Cancel a Parallel.For or ForEach Loop 描述如何通过使用 System.Threading.CancellationToken 取消并行循环Describes how to cancel a parallel loop by using a System.Threading.CancellationToken
如何:加快小型循环主体的速度How to: Speed Up Small Loop Bodies 描述在循环主体极小时加快执行速度的方法。Describes one way to speed up execution when a loop body is very small.
任务并行库 (TPL)Task Parallel Library (TPL) 提供任务并行库的概述。Provides an overview of the Task Parallel Library.
并行编程Parallel Programming 介绍.NET Framework 中的并行编程。Introduces Parallel Programming in the .NET Framework.

请参阅See also