Barrier 클래스
정의
여러 작업이 여러 단계에 걸쳐 특정 알고리즘에서 병렬로 함께 작동할 수 있도록 합니다.Enables multiple tasks to cooperatively work on an algorithm in parallel through multiple phases.
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
- 상속
-
Barrier
- 특성
- 구현
예제
다음 예제에서는 장벽을 사용 하는 방법을 보여 줍니다.The following example shows how to use a barrier:
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
설명
일련의 단계를 통해 이동 하 여 작업 그룹을 공동으로 지정 합니다. 여기서 그룹의 각 단계는 지정 된 단계에서에 도착 했음을 신호로 알리고 Barrier 다른 모든 사용자가 도착할 때까지 암시적으로 대기 합니다.A group of tasks cooperate by moving through a series of phases, where each in the group signals it has arrived at the Barrier in a given phase and implicitly waits for all others to arrive. 여러 단계에서 동일 하 Barrier 게 사용할 수 있습니다.The same Barrier can be used for multiple phases.
생성자
Barrier(Int32) |
Barrier 클래스의 새 인스턴스를 초기화합니다.Initializes a new instance of the Barrier class. |
Barrier(Int32, Action<Barrier>) |
Barrier 클래스의 새 인스턴스를 초기화합니다.Initializes a new instance of the Barrier class. |
속성
CurrentPhaseNumber |
장벽의 현재 단계 번호를 가져옵니다.Gets the number of the barrier's current phase. |
ParticipantCount |
장벽에 있는 참가자의 총 수를 가져옵니다.Gets the total number of participants in the barrier. |
ParticipantsRemaining |
현재 단계에서 아직 신호를 받지 않은 장벽의 참가자 수를 가져옵니다.Gets the number of participants in the barrier that haven't yet signaled in the current phase. |
메서드
AddParticipant() |
추가 참가자가 있음을 Barrier에 알립니다.Notifies the Barrier that there will be an additional participant. |
AddParticipants(Int32) |
추가 참가자가 있음을 Barrier에 알립니다.Notifies the Barrier that there will be additional participants. |
Dispose() |
Barrier 클래스의 현재 인스턴스에서 사용하는 모든 리소스를 해제합니다.Releases all resources used by the current instance of the Barrier class. |
Dispose(Boolean) |
Barrier에서 사용하는 관리되지 않는 리소스를 해제하고, 관리되는 리소스를 선택적으로 해제할 수 있습니다.Releases the unmanaged resources used by the Barrier, and optionally releases the managed resources. |
Equals(Object) |
지정된 개체가 현재 개체와 같은지 확인합니다.Determines whether the specified object is equal to the current object. (다음에서 상속됨 Object) |
GetHashCode() |
기본 해시 함수로 작동합니다.Serves as the default hash function. (다음에서 상속됨 Object) |
GetType() |
현재 인스턴스의 Type을 가져옵니다.Gets the Type of the current instance. (다음에서 상속됨 Object) |
MemberwiseClone() |
현재 Object의 단순 복사본을 만듭니다.Creates a shallow copy of the current Object. (다음에서 상속됨 Object) |
RemoveParticipant() |
참가자가 하나 감소함을 Barrier에 알립니다.Notifies the Barrier that there will be one less participant. |
RemoveParticipants(Int32) |
참가자가 감소함을 Barrier에 알립니다.Notifies the Barrier that there will be fewer participants. |
SignalAndWait() |
참가자가 장벽에 도달했다는 신호를 보내고 다른 모든 참가자도 장벽에 도달할 때까지 기다립니다.Signals that a participant has reached the barrier and waits for all other participants to reach the barrier as well. |
SignalAndWait(CancellationToken) |
참가자가 장벽에 도달했다는 신호를 보내고 취소 토큰을 확인하면서 다른 모든 참가자도 장벽에 도달할 때까지 기다립니다.Signals that a participant has reached the barrier and waits for all other participants to reach the barrier, while observing a cancellation token. |
SignalAndWait(Int32) |
참가자가 장벽에 도달했다는 신호를 보내고 부호 있는 32비트 정수로 시간 제한을 측정하여 다른 모든 참가자도 장벽에 도달할 때까지 기다립니다.Signals that a participant has reached the barrier and waits for all other participants to reach the barrier as well, using a 32-bit signed integer to measure the timeout. |
SignalAndWait(Int32, CancellationToken) |
참가자가 장벽에 도달했다는 신호를 보내고 취소 토큰을 확인하면서 부호 있는 32비트 정수로 시간 제한을 측정하여 다른 모든 참가자도 장벽에 도달할 때까지 기다립니다.Signals that a participant has reached the barrier and waits for all other participants to reach the barrier as well, using a 32-bit signed integer to measure the timeout, while observing a cancellation token. |
SignalAndWait(TimeSpan) |
참가자가 장벽에 도달했다는 신호를 보내고 TimeSpan 개체를 사용하여 시간 간격을 측정하여 다른 참가자도 장벽에 도달할 때까지 기다립니다.Signals that a participant has reached the barrier and waits for all other participants to reach the barrier as well, using a TimeSpan object to measure the time interval. |
SignalAndWait(TimeSpan, CancellationToken) |
참가자가 장벽에 도달했다는 신호를 보내고 취소 토큰을 확인하면서 TimeSpan 개체를 사용하여 시간 제한을 측정하여 다른 모든 참가자도 장벽에 도달할 때까지 기다립니다.Signals that a participant has reached the barrier and waits for all other participants to reach the barrier as well, using a TimeSpan object to measure the time interval, while observing a cancellation token. |
ToString() |
현재 개체를 나타내는 문자열을 반환합니다.Returns a string that represents the current object. (다음에서 상속됨 Object) |
적용 대상
스레드 보안
의 모든 public 및 protected 멤버 Barrier 는 스레드로부터 안전 하며 여러 스레드에서 동시에 사용 될 수 있습니다. Dispose는 예외입니다. Dispose는의 다른 모든 작업이 완료 된 경우에만 사용 해야 합니다 Barrier .All public and protected members of Barrier are thread-safe and may be used concurrently from multiple threads, with the exception of Dispose, which must only be used when all other operations on the Barrier have completed.