| 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 | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Barrier (.NET Framework) |
03/30/2017 |
.net |
dotnet-standard |
article |
|
613a8bc7-6a28-4795-bd6c-1abd9050478f |
13 |
rpetrusha |
ronpet |
wpickett |
Barrier (.NET Framework)
A barrier is a user-defined synchronization primitive that enables multiple threads (known as participants) to work concurrently on an algorithm in phases. Each participant executes until it reaches the barrier point in the code. The barrier represents the end of one phase of work. When a participant reaches the barrier, it blocks until all participants have reached the same barrier. After all participants have reached the barrier, you can optionally invoke a post-phase action. This post-phase action can be used to perform actions by a single thread while all other threads are still blocked. After the action has been executed, the participants are all unblocked.
The following code snippet shows a basic barrier pattern.
[!code-csharpCDS_Barrier#02] [!code-vbCDS_Barrier#02]
For a complete example, see How to: Synchronize Concurrent Operations with a Barrier.
Adding and Removing Participants
When you create a xref:System.Threading.Barrier, specify the number of participants. You can also add or remove participants dynamically at any time. For example, if one participant solves its part of the problem, you can store the result, stop execution on that thread, and call xref:System.Threading.Barrier.RemoveParticipant%2A to decrement the number of participants in the barrier. When you add a participant by calling xref:System.Threading.Barrier.AddParticipant%2A, the return value specifies the current phase number, which may be useful in order to initialize the work of the new participant.
Broken Barriers
Deadlocks can occur if one participant fails to reach the barrier. To avoid these deadlocks, use the overloads of the xref:System.Threading.Barrier.SignalAndWait%2A method to specify a time-out period and a cancellation token. These overloads return a Boolean value that every participant can check before it continues to the next phase.
Post-Phase Exceptions
If the post-phase delegate throws an exception, it is wrapped in a xref:System.Threading.BarrierPostPhaseException object which is then propagated to all participants.
Barrier Versus ContinueWhenAll
Barriers are especially useful when the threads are performing multiple phases in loops. If your code requires only one or two phases of work, consider whether to use xref:System.Threading.Tasks.Task?displayProperty=nameWithType objects with any kind of implicit join, including:
-
xref:System.Threading.Tasks.TaskFactory.ContinueWhenAll%2A
-
xref:System.Threading.Tasks.Parallel.Invoke%2A
-
xref:System.Threading.Tasks.Parallel.ForEach%2A
-
xref:System.Threading.Tasks.Parallel.For%2A
For more information, see Chaining Tasks by Using Continuation Tasks.
See Also
Threading Objects and Features
How to: Synchronize Concurrent Operations with a Barrier