C# Class System.Threading.Barrier

Inheritance: IDisposable
显示文件 Open project: dotnet/corefx Class Usage Examples

Public Methods

Method Description
AddParticipant ( ) : long

Notifies the Barrier that there will be an additional participant.

AddParticipants ( int participantCount ) : long

Notifies the Barrier that there will be additional participants.

Barrier ( int participantCount ) : System.Diagnostics

Initializes a new instance of the Barrier class.

Barrier ( int participantCount, Action postPhaseAction ) : System.Diagnostics

Initializes a new instance of the Barrier class.

The postPhaseAction delegate will be executed after all participants have arrived at the barrier in one phase. The participants will not be released to the next phase until the postPhaseAction delegate has completed execution.

Dispose ( ) : void

Releases all resources used by the current instance of Barrier.

Unlike most of the members of Barrier, Dispose is not thread-safe and may not be used concurrently with other members of this instance.

RemoveParticipant ( ) : void

Notifies the Barrier that there will be one less participant.

RemoveParticipants ( int participantCount ) : void

Notifies the Barrier that there will be fewer participants.

SignalAndWait ( TimeSpan timeout ) : Boolean

Signals that a participant has reached the Barrier and waits for all other participants to reach the barrier as well, using a T:System.TimeSpan to measure the time interval.

SignalAndWait ( TimeSpan timeout, CancellationToken cancellationToken ) : Boolean

Signals that a participant has reached the Barrier and waits for all other participants to reach the barrier as well, using a T:System.TimeSpan to measure the time interval, while observing a .

SignalAndWait ( int millisecondsTimeout ) : bool

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 time interval.

SignalAndWait ( int millisecondsTimeout, CancellationToken cancellationToken ) : bool

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 time interval, while observing a .

SignalAndWait ( ) : void

Signals that a participant has reached the Barrier and waits for all other participants to reach the barrier as well.

SignalAndWait ( CancellationToken cancellationToken ) : void

Signals that a participant has reached the Barrier and waits for all other participants to reach the barrier, while observing a .

Protected Methods

Method Description
Dispose ( bool disposing ) : void

When overridden in a derived class, releases the unmanaged resources used by the Barrier, and optionally releases the managed resources.

Unlike most of the members of Barrier, Dispose is not thread-safe and may not be used concurrently with other members of this instance.

Private Methods

Method Description
DiscontinuousWait ( ManualResetEventSlim currentPhaseEvent, int totalTimeout, CancellationToken token, long observedPhase ) : bool

The reason of discontinuous waiting instead of direct waiting on the event is to avoid the race where the sense is changed twice because the next phase is finished (due to either RemoveParticipant is called or another thread joined the next phase instead of the current thread) so the current thread will be stuck on the event because it is reset back The maxwait and the shift numbers are arbitrarily choosen, there were no references picking them

FinishPhase ( bool observedSense ) : void
GetCurrentTotal ( int currentTotal, int &current, int &total, bool &sense ) : void

Extract the three variables current, total and sense from a given big variable

InvokePostPhaseAction ( object obj ) : void
SetCurrentTotal ( int currentTotal, int current, int total, bool sense ) : bool

Write the three variables current. total and the sense to the m_currentTotal

SetResetEvents ( bool observedSense ) : void

Sets the current phase event and reset the next phase event

ThrowIfDisposed ( ) : void

Throw ObjectDisposedException if the barrier is disposed

WaitCurrentPhase ( ManualResetEventSlim currentPhaseEvent, long observedPhase ) : void

Wait until the current phase finishes completely by spinning until either the event is set, or the phase count is incremented more than one time

Method Details

AddParticipant() public method

Notifies the Barrier that there will be an additional participant.
/// Adding a participant would cause the barrier's participant count to /// exceed . /// /// The method was invoked from within a post-phase action. /// The current instance has already been /// disposed.
public AddParticipant ( ) : long
return long

AddParticipants() public method

Notifies the Barrier that there will be additional participants.
is less than /// 0. Adding participants would cause the /// barrier's participant count to exceed . /// The method was invoked from within a post-phase action. /// The current instance has already been /// disposed.
public AddParticipants ( int participantCount ) : long
participantCount int The number of additional participants to add to the /// barrier.
return long

Barrier() public method

Initializes a new instance of the Barrier class.
is less than 0 /// or greater than .
public Barrier ( int participantCount ) : System.Diagnostics
participantCount int The number of participating threads.
return System.Diagnostics

Barrier() public method

Initializes a new instance of the Barrier class.
The postPhaseAction delegate will be executed after all participants have arrived at the barrier in one phase. The participants will not be released to the next phase until the postPhaseAction delegate has completed execution.
is less than 0 /// or greater than .
public Barrier ( int participantCount, Action postPhaseAction ) : System.Diagnostics
participantCount int The number of participating threads.
postPhaseAction Action The to be executed after each /// phase.
return System.Diagnostics

Dispose() public method

Releases all resources used by the current instance of Barrier.
Unlike most of the members of Barrier, Dispose is not thread-safe and may not be used concurrently with other members of this instance.
/// The method was invoked from within a post-phase action. ///
public Dispose ( ) : void
return void

Dispose() protected method

When overridden in a derived class, releases the unmanaged resources used by the Barrier, and optionally releases the managed resources.
Unlike most of the members of Barrier, Dispose is not thread-safe and may not be used concurrently with other members of this instance.
protected Dispose ( bool disposing ) : void
disposing bool true to release both managed and unmanaged resources; false to release /// only unmanaged resources.
return void

RemoveParticipant() public method

Notifies the Barrier that there will be one less participant.
The barrier already has 0 /// participants. /// The method was invoked from within a post-phase action. /// The current instance has already been /// disposed.
public RemoveParticipant ( ) : void
return void

RemoveParticipants() public method

Notifies the Barrier that there will be fewer participants.
is less than /// 0. The barrier already has 0 participants. /// The method was invoked from within a post-phase action. /// The current instance has already been /// disposed.
public RemoveParticipants ( int participantCount ) : void
participantCount int The number of additional participants to remove from the barrier.
return void

SignalAndWait() public method

Signals that a participant has reached the Barrier and waits for all other participants to reach the barrier as well, using a T:System.TimeSpan to measure the time interval.
is a negative number /// other than -1 milliseconds, which represents an infinite time-out, or it is greater than /// . /// The method was invoked from within a post-phase action, the barrier currently has 0 participants, /// or the barrier is being used by more threads than are registered as participants. /// The current instance has already been /// disposed.
public SignalAndWait ( TimeSpan timeout ) : Boolean
timeout TimeSpan A that represents the number of /// milliseconds to wait, or a that represents -1 milliseconds to /// wait indefinitely.
return Boolean

SignalAndWait() public method

Signals that a participant has reached the Barrier and waits for all other participants to reach the barrier as well, using a T:System.TimeSpan to measure the time interval, while observing a .
is a negative number /// other than -1 milliseconds, which represents an infinite time-out. /// The method was invoked from within a post-phase action, the barrier currently has 0 participants, /// or the barrier is being used by more threads than are registered as participants. /// has been /// canceled. The current instance has already been /// disposed.
public SignalAndWait ( TimeSpan timeout, CancellationToken cancellationToken ) : Boolean
timeout TimeSpan A that represents the number of /// milliseconds to wait, or a that represents -1 milliseconds to /// wait indefinitely.
cancellationToken CancellationToken The to /// observe.
return Boolean

SignalAndWait() public method

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 time interval.
is a /// negative number other than -1, which represents an infinite time-out. /// The method was invoked from within a post-phase action, the barrier currently has 0 participants, /// or the barrier is being used by more threads than are registered as participants. /// The current instance has already been /// disposed.
public SignalAndWait ( int millisecondsTimeout ) : bool
millisecondsTimeout int The number of milliseconds to wait, or (-1) to wait indefinitely.
return bool

SignalAndWait() public method

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 time interval, while observing a .
is a /// negative number other than -1, which represents an infinite time-out. /// The method was invoked from within a post-phase action, the barrier currently has 0 participants, /// or the barrier is being used by more threads than are registered as participants. /// has been /// canceled. The current instance has already been /// disposed.
public SignalAndWait ( int millisecondsTimeout, CancellationToken cancellationToken ) : bool
millisecondsTimeout int The number of milliseconds to wait, or (-1) to wait indefinitely.
cancellationToken CancellationToken The to /// observe.
return bool

SignalAndWait() public method

Signals that a participant has reached the Barrier and waits for all other participants to reach the barrier as well.
/// The method was invoked from within a post-phase action, the barrier currently has 0 participants, /// or the barrier is being used by more threads than are registered as participants. /// The current instance has already been /// disposed.
public SignalAndWait ( ) : void
return void

SignalAndWait() public method

Signals that a participant has reached the Barrier and waits for all other participants to reach the barrier, while observing a .
/// The method was invoked from within a post-phase action, the barrier currently has 0 participants, /// or the barrier is being used by more threads than are registered as participants. /// has been /// canceled. The current instance has already been /// disposed.
public SignalAndWait ( CancellationToken cancellationToken ) : void
cancellationToken CancellationToken The to /// observe.
return void