Semaphore — Concurrency Use Case: I saw you listed that you were managing concurrency using Semaphore — what’s the use case for that? Multithreading?
Dodge Construction Network · Senior Developer · technical
Published
Asked in 2026 · C#, System Design
Community answers
Posted by wasAsked editorial collection
A semaphore limits how many callers can enter a protected region at once. Unlike a lock, which normally allows one owner, a semaphore can allow N concurrent operations. `SemaphoreSlim` is useful for in-process asynchronous throttling—for example, limiting calls to a downstream API or the number of files processed at once.
```csharp
private readonly SemaphoreSlim gate = new(initialCount: 4);
public async Task<string> FetchAsync(string path, CancellationToken ct)
{
await gate.WaitAsync(ct);
try
{
return await http.GetStringAsync(path, ct);
}
finally
{
gate.Release();
}
}
```
Always release in `finally`. This limits concurrency but does not make shared mutable state safe by itself. It also protects only one process; multiple application instances need a downstream-enforced limit, distributed coordination, or queue. Avoid creating a new semaphore per call because that provides no shared limit. For large producer/consumer flows, a bounded `Channel<T>` often expresses backpressure more clearly.
Pooja Parmar · · 0 likes
A semaphore limits how many callers can access a resource or run a section of code at the same time. In .NET, SemaphoreSlim is the lightweight, async-friendly version used inside one process (Semaphore is the heavier OS-level type that can work across processes).
Typical use cases
1. Throttling concurrent calls to an external API or resource
A third-party API allows only 5 concurrent requests, or you want to avoid overloading a database:
private static readonly SemaphoreSlim _gate = new(5);
public async Task<Result> CallPartnerAsync(Request req, CancellationToken ct)
{
await _gate.WaitAsync(ct);
try { return await _partnerClient.SendAsync(req, ct); }
finally { _gate.Release(); }
}
2. Async mutual exclusion (an async lock)
The lock statement cannot contain await. SemaphoreSlim(1, 1) provides the same exclusive access for async code — for example refreshing a cached access token so only one request refreshes while others wait:
private readonly SemaphoreSlim _refreshLock = new(1, 1);
public async Task<string> GetTokenAsync(CancellationToken ct)
{
if (!IsExpired(_token)) return _token!;
await _refreshLock.WaitAsync(ct);
try
{
if (IsExpired(_token)) _token = await FetchNewTokenAsync(ct); // double-check inside the lock
return _token!;
}
finally { _refreshLock.Release(); }
}
3. Limiting parallel batch processing
Processing thousands of files or messages with Task.WhenAll but at most N at once (Parallel.ForEachAsync with MaxDegreeOfParallelism is often simpler for this case).
4. Preventing cache stampedes
Allow only one request to rebuild an expensive cache entry.
Is it multithreading?
It is a concurrency-control tool. It works with multiple threads, and it works just as well with asynchronous operations that are not tied to threads. In an ASP.NET Core app, many requests run concurrently on pool threads, so a semaphore coordinates them.
Rules
- Always release in a finally block.
- Pass a CancellationToken or timeout to WaitAsync to avoid waiting forever.
- A SemaphoreSlim only coordinates within one process. With several app instances, use a distributed mechanism (Redis lock, database lock, or queue-based concurrency) or rate limiting at the gateway.