Skip to content

Latest commit

 

History

History
52 lines (33 loc) · 1.24 KB

File metadata and controls

52 lines (33 loc) · 1.24 KB

AsyncGate

Controls concurrent access to a shared resource like a gate: Enter grants user access, while DisposeAsync closes the gate and asynchronously waits for all users to leave.

Members

Enter

public IDisposable Enter();

Permits one user to enter, or throws ObjectDisposedException if the gate is closed (or in the process of being closed).

Returns an IDisposable which must be disposed when the user is done. It is recommend that all calls to Enter happen in a using block (see example below).

DisposeAsync

public ValueTask DisposeAsync();

Closes the gate (if not already closed) and asynchronously waits for all existing users to leave.

Once this method returns, it is guaranteed nobody is accessing the shared resource.

⚠️ If a user fails to leave, this will hang forever.

Examples

The gate is defined as follows.

var gate = new AsyncGate();

All access to the shared resource happens inside the gate.

using (gate.Enter())
{
    // Access shared resource here.
}

When the gate is closed, it is guaranteed nobody is accessing the shared resource.

await gate.DisposeAsync();

// It is now guaranteed that nobody is accessing the shared resource.