Show / Hide Table of Contents

Class AsyncBarrier

Enables multiple tasks to cooperatively work on an algorithm in parallel through multiple phases.

Inheritance
object
Disposable
AsyncBarrier
Implements
IAsyncEvent
IDisposable
IResettable
Inherited Members
Disposable.IsDisposed
Disposable.IsDisposing
Disposable.IsDisposingOrDisposed
Disposable.CreateException()
Disposable.DisposedTask
Disposable.GetDisposedTask<T>()
Disposable.TrySetDisposedException<T>(TaskCompletionSource<T>)
Disposable.TrySetDisposedException(TaskCompletionSource)
Disposable.DisposeAsync()
Disposable.TryBeginDispose()
Disposable.Dispose()
Disposable.Dispose(IEnumerable<IDisposable>)
Disposable.DisposeAsync(IEnumerable<IAsyncDisposable>)
Disposable.Dispose<T>(ReadOnlySpan<T>)
Disposable.DisposeAsync(params IAsyncDisposable[])
object.Equals(object)
object.Equals(object, object)
object.GetHashCode()
object.GetType()
object.MemberwiseClone()
object.ReferenceEquals(object, object)
object.ToString()
Namespace: DotNext.Threading
Assembly: DotNext.Threading.dll
Syntax
public class AsyncBarrier : Disposable, IAsyncEvent, IDisposable, IResettable
Remarks

This is asynchronous version of Barrier with small differences:

  • Post-phase action is presented by virtual method PostPhase(long).
  • It is possible to wait for phase completion without signal.
  • It is possible to signal without waiting of phase completion.
  • Post-phase action is asynchronous.
  • Number of phases is limited by long data type.

Constructors

| Edit this page View Source

AsyncBarrier(long)

Initializes a new Barrier withe given number of participating tasks.

Declaration
public AsyncBarrier(long participantCount)
Parameters
Type Name Description
long participantCount

The number of participating tasks.

Exceptions
Type Condition
ArgumentOutOfRangeException

participantCount is less than 0.

Properties

| Edit this page View Source

CurrentPhaseNumber

Gets the number of the barrier's current phase.

Declaration
public long CurrentPhaseNumber { get; }
Property Value
Type Description
long
| Edit this page View Source

ParticipantCount

Gets the total number of participants in the barrier.

Declaration
public long ParticipantCount { get; }
Property Value
Type Description
long
| Edit this page View Source

ParticipantsRemaining

Gets the number of participants in the barrier that haven't yet signaled in the current phase.

Declaration
public long ParticipantsRemaining { get; }
Property Value
Type Description
long

Methods

| Edit this page View Source

AddParticipant()

Notifies this barrier that there will be additional participant.

Declaration
public long AddParticipant()
Returns
Type Description
long

The phase number of the barrier in which the new participants will first participate.

Exceptions
Type Condition
ObjectDisposedException

The current instance has already been disposed.

| Edit this page View Source

AddParticipants(long)

Notifies this barrier that there will be additional participants.

Declaration
public long AddParticipants(long participantCount)
Parameters
Type Name Description
long participantCount

The number of additional participants to add to the barrier.

Returns
Type Description
long

The phase number of the barrier in which the new participants will first participate.

Exceptions
Type Condition
ArgumentOutOfRangeException

participantCount is less than 0.

ObjectDisposedException

The current instance has already been disposed.

| Edit this page View Source

Dispose(bool)

Releases managed and unmanaged resources associated with this object.

Declaration
protected override void Dispose(bool disposing)
Parameters
Type Name Description
bool disposing

true if called from Dispose(); false if called from finalizer ~Disposable().

Overrides
Disposable.Dispose(bool)
| Edit this page View Source

DisposeAsyncCore()

Releases managed resources associated with this object asynchronously.

Declaration
protected override ValueTask DisposeAsyncCore()
Returns
Type Description
ValueTask

The task representing asynchronous execution of this method.

Overrides
Disposable.DisposeAsyncCore()
Remarks

This method makes sense only if derived class implements IAsyncDisposable interface.

| Edit this page View Source

PostPhase(long)

The action to be executed after each phase.

Declaration
protected virtual ValueTask PostPhase(long phase)
Parameters
Type Name Description
long phase

The current phase number.

Returns
Type Description
ValueTask

A task representing post-phase asynchronous execution.

| Edit this page View Source

RemoveParticipant()

Notifies this barrier that there will be one less participant.

Declaration
public void RemoveParticipant()
Remarks

This method may resume all tasks suspended by WaitAsync(TimeSpan, CancellationToken) and SignalAndWaitAsync(TimeSpan, CancellationToken) methods.

Exceptions
Type Condition
ObjectDisposedException

The current instance has already been disposed.

| Edit this page View Source

RemoveParticipants(long)

Notifies this barrier that there will be fewer participants.

Declaration
public void RemoveParticipants(long participantCount)
Parameters
Type Name Description
long participantCount

The number of additional participants to remove from the barrier.

Remarks

This method may resume all tasks suspended by WaitAsync(TimeSpan, CancellationToken) and SignalAndWaitAsync(TimeSpan, CancellationToken) methods.

Exceptions
Type Condition
ArgumentOutOfRangeException

participantCount less than 1 or greater that ParticipantsRemaining.

ObjectDisposedException

The current instance has already been disposed.

| Edit this page View Source

SignalAndWaitAsync(CancellationToken)

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

Declaration
public ValueTask SignalAndWaitAsync(CancellationToken token = default)
Parameters
Type Name Description
CancellationToken token

The token that can be used to cancel the waiting operation.

Returns
Type Description
ValueTask

The task representing waiting operation.

Exceptions
Type Condition
ObjectDisposedException

The current instance has already been disposed.

BarrierPostPhaseException

PostPhase(long) fails.

| Edit this page View Source

SignalAndWaitAsync(TimeSpan, CancellationToken)

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

Declaration
public ValueTask<bool> SignalAndWaitAsync(TimeSpan timeout, CancellationToken token = default)
Parameters
Type Name Description
TimeSpan timeout

The time to wait for phase completion.

CancellationToken token

The token that can be used to cancel the waiting operation.

Returns
Type Description
ValueTask<bool>

true if all other participants reached the barrier; otherwise, false.

Exceptions
Type Condition
ObjectDisposedException

The current instance has already been disposed.

BarrierPostPhaseException

PostPhase(long) fails.

| Edit this page View Source

WaitAsync(CancellationToken)

Waits for all other participants to reach the barrier.

Declaration
public ValueTask WaitAsync(CancellationToken token = default)
Parameters
Type Name Description
CancellationToken token

The token that can be used to cancel the waiting operation.

Returns
Type Description
ValueTask

The task representing asynchronous result.

Exceptions
Type Condition
ObjectDisposedException

The current instance has already been disposed.

OperationCanceledException

The operation has been canceled.

| Edit this page View Source

WaitAsync(TimeSpan, CancellationToken)

Waits for all other participants to reach the barrier.

Declaration
public ValueTask<bool> WaitAsync(TimeSpan timeout, CancellationToken token = default)
Parameters
Type Name Description
TimeSpan timeout

The time to wait for phase completion.

CancellationToken token

The token that can be used to cancel the waiting operation.

Returns
Type Description
ValueTask<bool>

true if all other participants reached the barrier; otherwise, false.

Exceptions
Type Condition
ObjectDisposedException

The current instance has already been disposed.

OperationCanceledException

The operation has been canceled.

Implements

IAsyncEvent
IDisposable
IResettable

Extension Methods

BasicExtensions.As<T>(T)
BasicExtensions.GetUserData<T>(T)
BasicExtensions.IsBetween<T, TLowerBound, TUpperBound>(T, TLowerBound, TUpperBound)
BasicExtensions.IsOneOf<T>(T, ReadOnlySpan<T>)
ExpressionBuilder.Const<T>(T)
AsyncLockAcquisition.AcquireLockAsync<T>(T, CancellationToken)
AsyncLockAcquisition.AcquireLockAsync<T>(T, TimeSpan, CancellationToken)
AsyncLockAcquisition.AcquireReadLockAsync<T>(T, CancellationToken)
AsyncLockAcquisition.AcquireReadLockAsync<T>(T, TimeSpan, CancellationToken)
AsyncLockAcquisition.AcquireWriteLockAsync<T>(T, bool, CancellationToken)
AsyncLockAcquisition.AcquireWriteLockAsync<T>(T, bool, TimeSpan, CancellationToken)
AsyncLockAcquisition.AcquireWriteLockAsync<T>(T, CancellationToken)
AsyncLockAcquisition.AcquireWriteLockAsync<T>(T, TimeSpan, CancellationToken)
LockAcquisition.AcquireReadLock<T>(T)
LockAcquisition.AcquireReadLock<T>(T, TimeSpan)
LockAcquisition.AcquireUpgradeableReadLock<T>(T)
LockAcquisition.AcquireUpgradeableReadLock<T>(T, TimeSpan)
LockAcquisition.AcquireWriteLock<T>(T)
LockAcquisition.AcquireWriteLock<T>(T, TimeSpan)
  • Edit this page
  • View Source
☀
☾
In this article
Back to top
Supported by the .NET Foundation
☀
☾