資料平行處理原則 (工作平行程式庫)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. 這個類別針對 forforeach 迴圈 (在 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.

注意

您也可以提供您自己的自訂 Partitioner 或排程器。You can also supply your own custom partitioner or scheduler. 如需詳細資訊,請參閱 PLINQ 和 TPL 的自訂 Partitioner工作排程器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. 啟用這個功能的 Helper 類型包括 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. 如需詳細資訊,請參閱 Parallel LINQ (PLINQ)For more information, see Parallel LINQ (PLINQ).

標題Title 描述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.
如何:撰寫含有 Partition-Local 變數的 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