Class AsyncBarrier
Enables multiple tasks to cooperatively work on an algorithm in parallel through multiple phases.
Namespace: DotNext.Threading
Assembly: DotNext.Threading.dll
Syntax
public class AsyncBarrier : Disposable, IAsyncEvent, ISynchronizer, IDisposable
Remarks
This is asynchronous version of
- Post-phase action is presented by virtual method PostPhase(Int64).
- It it 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
data type.
Constructors
| Improve this Doc View SourceAsyncBarrier(Int64)
Initializes a new Barrier withe given number of participating tasks.
Declaration
public AsyncBarrier(long participantCount)
Parameters
| Type | Name | Description |
|---|---|---|
| Int64 | participantCount | The number of participating tasks. |
Properties
| Improve this Doc View SourceCurrentPhaseNumber
Gets the number of the barrier's current phase.
Declaration
public long CurrentPhaseNumber { get; }
Property Value
| Type | Description |
|---|---|
| Int64 |
ParticipantCount
Gets the total number of participants in the barrier.
Declaration
public long ParticipantCount { get; }
Property Value
| Type | Description |
|---|---|
| Int64 |
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 |
|---|---|
| Int64 |
Methods
| Improve this Doc View SourceAddParticipant()
Notifies this barrier that there will be additional participant.
Declaration
public long AddParticipant()
Returns
| Type | Description |
|---|---|
| Int64 | The phase number of the barrier in which the new participants will first participate. |
AddParticipants(Int64)
Notifies this barrier that there will be additional participants.
Declaration
public long AddParticipants(long participantCount)
Parameters
| Type | Name | Description |
|---|---|---|
| Int64 | participantCount | The number of additional participants to add to the barrier. |
Returns
| Type | Description |
|---|---|
| Int64 | The phase number of the barrier in which the new participants will first participate. |
Dispose(Boolean)
Releases all resources associated with this barrier.
Declaration
protected override void Dispose(bool disposing)
Parameters
| Type | Name | Description |
|---|---|---|
| Boolean | disposing | Indicates whether the Dispose(Boolean) has been called directly or from finalizer. |
Remarks
This method is not thread-safe and may not be used concurrently with other members of this instance.
PostPhase(Int64)
The action to be executed after each phase.
Declaration
protected virtual Task PostPhase(long phase)
Parameters
| Type | Name | Description |
|---|---|---|
| Int64 | phase | The current phase number. |
Returns
| Type | Description |
|---|---|
| Task | A task representing post-phase asynchronous execution. |
RemoveParticipant()
Notifies this barrier that there will be one less participant.
Declaration
public void RemoveParticipant()
Remarks
This method may resume all tasks suspended by Wait(TimeSpan, CancellationToken) and SignalAndWait(TimeSpan, CancellationToken) methods.
RemoveParticipants(Int64)
Notifies this barrier that there will be fewer participants.
Declaration
public void RemoveParticipants(long participantCount)
Parameters
| Type | Name | Description |
|---|---|---|
| Int64 | participantCount | The number of additional participants to remove from the barrier. |
Remarks
This method may resume all tasks suspended by Wait(TimeSpan, CancellationToken) and SignalAndWait(TimeSpan, CancellationToken) methods.
SignalAndWait()
Signals that a participant has reached the barrier and waits for all other participants to reach the barrier as well.
Declaration
public Task SignalAndWait()
Returns
| Type | Description |
|---|---|
| Task | The task representing waiting operation. |
SignalAndWait(CancellationToken)
Signals that a participant has reached the barrier and waits for all other participants to reach the barrier as well.
Declaration
public Task SignalAndWait(CancellationToken token)
Parameters
| Type | Name | Description |
|---|---|---|
| CancellationToken | token | The token that can be used to cancel the waiting operation. |
Returns
| Type | Description |
|---|---|
| Task | The task representing waiting operation. |
SignalAndWait(TimeSpan)
Signals that a participant has reached the barrier and waits for all other participants to reach the barrier as well.
Declaration
public Task<bool> SignalAndWait(TimeSpan timeout)
Parameters
| Type | Name | Description |
|---|---|---|
| TimeSpan | timeout | The time to wait for phase completion. |
Returns
| Type | Description |
|---|---|
| Task<Boolean> | true if all other participants reached the barrier; otherwise, false. |
SignalAndWait(TimeSpan, CancellationToken)
Signals that a participant has reached the barrier and waits for all other participants to reach the barrier as well.
Declaration
public Task<bool> SignalAndWait(TimeSpan timeout, CancellationToken token)
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 |
|---|---|
| Task<Boolean> | true if all other participants reached the barrier; otherwise, false. |
Wait(TimeSpan, CancellationToken)
Waits for all other participants to reach the barrier.
Declaration
public Task<bool> Wait(TimeSpan timeout, CancellationToken token)
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 |
|---|---|
| Task<Boolean> | true if all other participants reached the barrier; otherwise, false. |
Explicit Interface Implementations
| Improve this Doc View SourceIAsyncEvent.IsSet
Declaration
bool IAsyncEvent.IsSet { get; }
Returns
| Type | Description |
|---|---|
| Boolean |
IAsyncEvent.Reset()
Declaration
bool IAsyncEvent.Reset()
Returns
| Type | Description |
|---|---|
| Boolean |
IAsyncEvent.Signal()
Declaration
bool IAsyncEvent.Signal()
Returns
| Type | Description |
|---|---|
| Boolean |
ISynchronizer.HasWaiters
Declaration
bool ISynchronizer.HasWaiters { get; }
Returns
| Type | Description |
|---|---|
| Boolean |