Permalink
Fetching contributors…
Cannot retrieve contributors at this time
32 lines (27 sloc) 2.25 KB
title ms.custom ms.date ms.prod ms.reviewer ms.suite ms.technology ms.tgt_pltfrm ms.topic helpviewer_keywords ms.assetid caps.latest.revision author ms.author manager
How to: Synchronize Concurrent Operations with a Barrier
03/30/2017
.net
dotnet-standard
article
Barrier, how to use
e1a253ff-e0fb-4df8-95ff-d01a90d4cb19
10
rpetrusha
ronpet
wpickett

How to: Synchronize Concurrent Operations with a Barrier

The following example shows how to synchronize concurrent tasks with a xref:System.Threading.Barrier.

Example

The purpose of the following program is to count how many iterations (or phases) are required for two threads to each find their half of the solution on the same phase by using a randomizing algorithm to reshuffle the words. After each thread has shuffled its words, the barrier post-phase operation compares the two results to see if the complete sentence has been rendered in correct word order.

[!code-csharpCDS_Barrier#01] [!code-vbCDS_Barrier#01]

A xref:System.Threading.Barrier is an object that prevents individual tasks in a parallel operation from continuing until all tasks reach the barrier. It is useful when a parallel operation occurs in phases, and each phase requires synchronization between tasks. In this example, there are two phases to the operation. In the first phase, each task fills its section of the buffer with data. When each task finishes filling its section, the task signals the barrier that it is ready to continue, and then waits. When all tasks have signaled the barrier, they are unblocked and the second phase starts. The barrier is necessary because the second phase requires that each task have access to all the data that has been generated to this point. Without the barrier, the first tasks to complete might try to read from buffers that have not been filled in yet by other tasks. You can synchronize any number of phases in this manner.

See Also

Data Structures for Parallel Programming