Barrier Osztály
Definíció
Fontos
Egyes információk olyan, kiadás előtti termékekre vonatkoznak, amelyek a kiadásig még jelentősen módosulhatnak. A Microsoft nem vállal kifejezett vagy törvényi garanciát az itt megjelenő információért.
Lehetővé teszi, hogy több tevékenység párhuzamosan, több fázison keresztül működjön együtt egy algoritmuson.
public ref class Barrier : IDisposable
public class Barrier : IDisposable
[System.Runtime.InteropServices.ComVisible(false)]
public class Barrier : IDisposable
type Barrier = class
interface IDisposable
[<System.Runtime.InteropServices.ComVisible(false)>]
type Barrier = class
interface IDisposable
Public Class Barrier
Implements IDisposable
- Öröklődés
-
Barrier
- Attribútumok
- Megvalósítás
Példák
Az alábbi példa egy akadály használatát mutatja be:
using System;
using System.Threading;
using System.Threading.Tasks;
class BarrierDemo
{
// Demonstrates:
// Barrier constructor with post-phase action
// Barrier.AddParticipants()
// Barrier.RemoveParticipant()
// Barrier.SignalAndWait(), incl. a BarrierPostPhaseException being thrown
static void BarrierSample()
{
int count = 0;
// Create a barrier with three participants
// Provide a post-phase action that will print out certain information
// And the third time through, it will throw an exception
Barrier barrier = new Barrier(3, (b) =>
{
Console.WriteLine("Post-Phase action: count={0}, phase={1}", count, b.CurrentPhaseNumber);
if (b.CurrentPhaseNumber == 2) throw new Exception("D'oh!");
});
// Nope -- changed my mind. Let's make it five participants.
barrier.AddParticipants(2);
// Nope -- let's settle on four participants.
barrier.RemoveParticipant();
// This is the logic run by all participants
Action action = () =>
{
Interlocked.Increment(ref count);
barrier.SignalAndWait(); // during the post-phase action, count should be 4 and phase should be 0
Interlocked.Increment(ref count);
barrier.SignalAndWait(); // during the post-phase action, count should be 8 and phase should be 1
// The third time, SignalAndWait() will throw an exception and all participants will see it
Interlocked.Increment(ref count);
try
{
barrier.SignalAndWait();
}
catch (BarrierPostPhaseException bppe)
{
Console.WriteLine("Caught BarrierPostPhaseException: {0}", bppe.Message);
}
// The fourth time should be hunky-dory
Interlocked.Increment(ref count);
barrier.SignalAndWait(); // during the post-phase action, count should be 16 and phase should be 3
};
// Now launch 4 parallel actions to serve as 4 participants
Parallel.Invoke(action, action, action, action);
// This (5 participants) would cause an exception:
// Parallel.Invoke(action, action, action, action, action);
// "System.InvalidOperationException: The number of threads using the barrier
// exceeded the total number of registered participants."
// It's good form to Dispose() a barrier when you're done with it.
barrier.Dispose();
}
}
Imports System.Threading
Imports System.Threading.Tasks
Module BarrierSample
' Demonstrates:
' Barrier constructor with post-phase action
' Barrier.AddParticipants()
' Barrier.RemoveParticipant()
' Barrier.SignalAndWait(), incl. a BarrierPostPhaseException being thrown
Sub Main()
Dim count As Integer = 0
' Create a barrier with three participants
' Provide a post-phase action that will print out certain information
' And the third time through, it will throw an exception
Dim barrier As New Barrier(3,
Sub(b)
Console.WriteLine("Post-Phase action: count={0}, phase={1}", count, b.CurrentPhaseNumber)
If b.CurrentPhaseNumber = 2 Then
Throw New Exception("D'oh!")
End If
End Sub)
' Nope -- changed my mind. Let's make it five participants.
barrier.AddParticipants(2)
' Nope -- let's settle on four participants.
barrier.RemoveParticipant()
' This is the logic run by all participants
Dim action As Action =
Sub()
Interlocked.Increment(count)
barrier.SignalAndWait()
' during the post-phase action, count should be 4 and phase should be 0
Interlocked.Increment(count)
barrier.SignalAndWait()
' during the post-phase action, count should be 8 and phase should be 1
' The third time, SignalAndWait() will throw an exception and all participants will see it
Interlocked.Increment(count)
Try
barrier.SignalAndWait()
Catch bppe As BarrierPostPhaseException
Console.WriteLine("Caught BarrierPostPhaseException: {0}", bppe.Message)
End Try
' The fourth time should be hunky-dory
Interlocked.Increment(count)
' during the post-phase action, count should be 16 and phase should be 3
barrier.SignalAndWait()
End Sub
' Now launch 4 parallel actions to serve as 4 participants
Parallel.Invoke(action, action, action, action)
' This (5 participants) would cause an exception:
' Parallel.Invoke(action, action, action, action, action)
' "System.InvalidOperationException: The number of threads using the barrier
' exceeded the total number of registered participants."
' It's good form to Dispose() a barrier when you're done with it.
barrier.Dispose()
End Sub
End Module
Megjegyzések
A feladatok egy csoportja több fázison halad át, ahol a csoport minden tagja azt jelzi, hogy egy adott fázisban érkezett Barrier , és implicit módon várja, amíg mindenki más megérkezik. Ugyanez Barrier több fázisban is használható.
Konstruktorok
| Name | Description |
|---|---|
| Barrier(Int32, Action<Barrier>) |
Inicializálja a Barrier osztály új példányát. |
| Barrier(Int32) |
Inicializálja a Barrier osztály új példányát. |
Tulajdonságok
| Name | Description |
|---|---|
| CurrentPhaseNumber |
Lekéri a sorompó aktuális fázisának számát. |
| ParticipantCount |
Lekéri a korlát résztvevőinek teljes számát. |
| ParticipantsRemaining |
Lekéri a jelenlegi fázisban még nem jelzett akadály résztvevőinek számát. |
Metódusok
| Name | Description |
|---|---|
| AddParticipant() |
Értesíti, Barrier hogy további résztvevő lesz. |
| AddParticipants(Int32) |
Értesíti, Barrier hogy további résztvevők lesznek. |
| Dispose() |
Az osztály aktuális példánya által használt összes erőforrást felszabadítja Barrier . |
| Dispose(Boolean) |
Felszabadítja a felügyelt erőforrások által Barrierhasznált nem felügyelt erőforrásokat, és opcionálisan felszabadítja a felügyelt erőforrásokat. |
| Equals(Object) |
Meghatározza, hogy a megadott objektum egyenlő-e az aktuális objektummal. (Öröklődés forrása Object) |
| GetHashCode() |
Ez az alapértelmezett kivonatoló függvény. (Öröklődés forrása Object) |
| GetType() |
Lekéri az Type aktuális példányt. (Öröklődés forrása Object) |
| MemberwiseClone() |
Az aktuális Objectpéldány sekély másolatát hozza létre. (Öröklődés forrása Object) |
| RemoveParticipant() |
Értesíti, Barrier hogy eggyel kevesebb résztvevő lesz. |
| RemoveParticipants(Int32) |
Értesíti, Barrier hogy kevesebb résztvevő lesz. |
| SignalAndWait() |
Azt jelzi, hogy egy résztvevő elérte a korlátot, és megvárja, amíg a többi résztvevő is eléri a korlátot. |
| SignalAndWait(CancellationToken) |
Azt jelzi, hogy egy résztvevő elérte a korlátot, és megvárja, amíg az összes többi résztvevő eléri a korlátot, miközben egy lemondási jogkivonatot figyel. |
| SignalAndWait(Int32, CancellationToken) |
Azt jelzi, hogy egy résztvevő elérte a korlátot, és megvárja, amíg az összes többi résztvevő eléri a korlátot, egy 32 bites aláírt egész szám használatával méri az időtúllépést, miközben egy lemondási jogkivonatot figyel. |
| SignalAndWait(Int32) |
Azt jelzi, hogy egy résztvevő elérte a korlátot, és megvárja, amíg az összes többi résztvevő eléri a korlátot, és egy 32 bites aláírt egész szám használatával méri az időtúllépést. |
| SignalAndWait(TimeSpan, CancellationToken) |
Azt jelzi, hogy egy résztvevő elérte a korlátot, és megvárja, amíg az összes többi résztvevő eléri a korlátot, egy objektummal TimeSpan méri az időintervallumot, miközben egy lemondási jogkivonatot figyel. |
| SignalAndWait(TimeSpan) |
Azt jelzi, hogy egy résztvevő elérte a korlátot, és megvárja, amíg az összes többi résztvevő eléri a korlátot is, egy objektummal TimeSpan méri az időintervallumot. |
| ToString() |
Az aktuális objektumot jelképező sztringet ad vissza. (Öröklődés forrása Object) |
A következőre érvényes:
Szálbiztonság
Az összes nyilvános és védett tagja Barrier szálbiztos, és egyidejűleg több szálból is használható, kivéve az Elidegenítést, amelyet csak akkor szabad használni, ha az összes többi művelet befejeződött.Barrier