FullStack Engineering CourseLesson 1 of 20 54 min
Async, Threads and Concurrency in .NET
Threads, the thread pool and why async exists
A thread is an operating system thread with its own stack (1 MB by default on Windows). Creating threads is expensive, so .NET keeps a thread pool (a set of ready-made worker threads) and lends them out for short jobs. ASP.NET Core runs each request on a thread pool thread. If that thread blocks while waiting for a database, it does nothing useful for the whole wait, and the pool has to find another thread for the next request.
There are two kinds of work:
- I/O-bound work (a SQL query, an HTTP call, reading a file) is mostly waiting for another machine or device. No thread needs to be busy during that wait, so async/await gives the thread back to the pool until the result arrives.
- CPU-bound work (resizing an image, hashing, parsing a large file) keeps a CPU core busy. Async does not make it faster, but Task.Run can move it off a thread that must stay responsive, such as a UI thread.
// I/O-bound: no thread is held while the query runs.
public async Task<Claim?> GetClaimAsync(int id, CancellationToken ct)
{
return await _db.Claims.AsNoTracking()
.FirstOrDefaultAsync(c => c.Id == id, ct);
}
// CPU-bound: the work itself needs a core. In a UI or a console app,
// Task.Run moves it to the thread pool so the caller stays free.
public Task<byte[]> HashFileAsync(byte[] content) =>
Task.Run(() => SHA256.HashData(content));In ASP.NET Core, do not wrap I/O in Task.Run. The request is already on a pool thread, so Task.Run just swaps one pool thread for another and adds overhead. Call the real async API instead (FirstOrDefaultAsync, ReadAsync, SendAsync). In a web app, use Task.Run only for CPU-bound work that you want to run in parallel with something else.
What await actually does
The compiler turns an async method into a state machine (a generated object that remembers where the method stopped). Each await is a point where the method can pause. If the awaited task is not finished, the method registers the rest of its code as a continuation (code to run when the task completes) and returns an unfinished Task to its caller. The thread is now free. When the I/O completes, the method resumes from the same point, possibly on a different thread, with its local variables restored.
public async Task<int> CountWordsAsync(string path)
{
Console.WriteLine($"Before await: thread {Environment.CurrentManagedThreadId}");
string text = await File.ReadAllTextAsync(path);
Console.WriteLine($"After await: thread {Environment.CurrentManagedThreadId}");
return text.Split(' ', StringSplitOptions.RemoveEmptyEntries).Length;
}
// Roughly what the compiler generates (simplified):
// struct CountWordsStateMachine : IAsyncStateMachine
// {
// public int state; // which await we are at
// public AsyncTaskMethodBuilder<int> builder;
// public string path; // parameters and locals become fields
// private TaskAwaiter<string> awaiter;
//
// public void MoveNext()
// {
// if (state == 0) goto Resume;
// awaiter = File.ReadAllTextAsync(path).GetAwaiter();
// if (!awaiter.IsCompleted)
// {
// state = 0;
// builder.AwaitUnsafeOnCompleted(ref awaiter, ref this); // continuation = MoveNext
// return; // the thread is released here
// }
// Resume:
// string text = awaiter.GetResult(); // rethrows if the task faulted
// builder.SetResult(text.Split(' ').Length);
// }
// }Before await: thread 1 After await: thread 7 (the thread numbers vary from run to run)
This has three consequences:
- An async method runs synchronously until its first await on an unfinished task, so code before that await runs on the caller's thread.
- If the awaited task is already complete (a cached value, for example), there is no pause and the method continues on the same thread.
- An exception inside an async method is stored in the returned Task and rethrown at the await. If the caller never awaits, it never sees the exception.
Where the continuation runs depends on the SynchronizationContext (an object that decides which thread runs code after an await). A WPF or WinForms UI thread has one that sends continuations back to the UI thread, so code after await can update controls. Classic ASP.NET (System.Web) had one per request. ASP.NET Core and console apps have none, so continuations run on any thread pool thread.
Task, Task<T> and ValueTask
A Task represents work that will finish later. Task is a class, so every Task is an object on the heap. An async method that has to wait allocates one (plus its state machine), and the garbage collector must clean them up later. ValueTask<T> is a struct, so it does not need a heap allocation. It holds either a result that is already available or a Task. A method that usually returns immediately (a cache hit, for example) then allocates nothing. One allocation costs almost nothing, but in code that runs millions of times (a hot path), the allocations pile up and slow the program down. The next lesson explains the heap, the stack and the garbage collector.
private readonly ConcurrentDictionary<int, Payer> _cache = new();
// Most calls hit the cache, so ValueTask saves an allocation per call.
public ValueTask<Payer> GetPayerAsync(int id, CancellationToken ct)
{
if (_cache.TryGetValue(id, out var payer))
return ValueTask.FromResult(payer); // no Task allocated
return new ValueTask<Payer>(LoadAndCacheAsync(id, ct));
}
private async Task<Payer> LoadAndCacheAsync(int id, CancellationToken ct)
{
var payer = await _repository.GetPayerAsync(id, ct);
_cache[id] = payer;
return payer;
}ValueTask has rules that Task does not:
- Await it exactly once.
- Do not await it twice, and do not await it from two places at once.
- Do not read .Result before it completes.
A ValueTask can be backed by a pooled object that gets reused as soon as you have consumed it. If you need to break any of these rules, call .AsTask() once and use the Task.
| Task / Task<T> | ValueTask / ValueTask<T> | |
|---|---|---|
| Type | Class (allocates when it completes asynchronously) | Struct (no allocation when the result is already available) |
| Await it more than once | Yes | No |
| Pass to Task.WhenAll / WhenAny | Yes | Only after .AsTask() |
| Use it for | The default for almost all async methods | Hot paths that usually complete synchronously, where profiling shows allocations matter |
ConfigureAwait and the classic deadlock
Blocking on async code with .Result or .Wait() can deadlock on a thread that has a single-threaded SynchronizationContext, such as a UI thread. The blocked thread waits for the task. The task's continuation is queued back to that same thread, so it waits for the thread. Neither can continue.
// WinForms / WPF / classic ASP.NET: this hangs forever.
private void LoadButton_Click(object sender, EventArgs e)
{
string data = GetDataAsync().Result; // 1. UI thread blocks here, waiting for the task
resultLabel.Text = data;
}
private async Task<string> GetDataAsync()
{
await Task.Delay(1000); // 2. continuation is posted back to the UI thread,
return "done"; // which is blocked in step 1: deadlock
}
// Fix 1 (the real fix): async all the way up.
private async void LoadButton_Click(object sender, EventArgs e) // async void is allowed for event handlers
{
resultLabel.Text = await GetDataAsync();
}
// Fix 2 (library code): don't capture the caller's context.
private async Task<string> GetDataInLibraryAsync()
{
await Task.Delay(1000).ConfigureAwait(false); // continuation runs on the thread pool
return "done";
}ConfigureAwait(false) tells the await not to return to the captured context. Library code that might be called from a UI app should use it on every await, because a library never needs the caller's context. ASP.NET Core has no SynchronizationContext, so ConfigureAwait(false) changes nothing in application code there, and .Result cannot cause this exact deadlock. But .Result still blocks a pool thread, which leads to thread pool starvation (covered below). The safe rule everywhere is async all the way: once a call chain is async, every caller up to the entry point awaits.
async void, fire-and-forget and exceptions
An async void method returns nothing the caller can await. The caller cannot tell when it finishes and cannot catch its exceptions. An unhandled exception in it goes to the SynchronizationContext, or to the thread pool when there is none, which crashes the process. Use async void only for event handlers, whose signature you cannot change. Everywhere else, return Task.
// Bad: an exception here crashes the process in ASP.NET Core.
public async void SendWelcomeEmail(User user) => await _email.SendAsync(user.Email, "Welcome");
// Good: the caller can await it, and exceptions flow to the caller.
public async Task SendWelcomeEmailAsync(User user, CancellationToken ct) =>
await _email.SendAsync(user.Email, "Welcome", ct);Fire-and-forget (starting a Task without awaiting it) has a quieter version of the same problem. Its exception is stored in a task nobody checks. In a web app, the work may still be running after the request's services (such as a scoped DbContext) have been disposed. If work must outlive the request, hand it to something that manages its lifetime: a queue read by a BackgroundService, an Azure Service Bus message or a Durable Function.
Task.WhenAll waits for every task. If one or more fail, await rethrows only the first exception. All of them are in the returned task's Exception property, so read it when you need every failure:
var tasks = claimIds.Select(id => _eligibility.CheckAsync(id, ct)).ToList();
Task<EligibilityResult[]> all = Task.WhenAll(tasks);
try
{
EligibilityResult[] results = await all;
}
catch
{
// await rethrew the first failure; all of them are here:
foreach (var ex in all.Exception!.InnerExceptions)
_logger.LogError(ex, "Eligibility check failed");
throw;
}Cancellation
Cancellation in .NET is cooperative: the code doing the work must check for it. A CancellationTokenSource creates a CancellationToken. The working code checks the token, or passes it to the APIs it calls, and they throw OperationCanceledException when it is cancelled. ASP.NET Core gives every request a token (HttpContext.RequestAborted) that fires when the client disconnects. Add a CancellationToken as the last parameter of every async method and pass it all the way down to EF Core and HttpClient.
[HttpGet("{id}")]
public async Task<ActionResult<ClaimDto>> Get(int id, CancellationToken ct) // bound to RequestAborted
{
var claim = await _service.GetClaimAsync(id, ct);
return claim is null ? NotFound() : Ok(claim);
}
// A timeout of our own, combined with the caller's token.
public async Task<EligibilityResult> CheckWithTimeoutAsync(int claimId, CancellationToken ct)
{
using var timeout = CancellationTokenSource.CreateLinkedTokenSource(ct);
timeout.CancelAfter(TimeSpan.FromSeconds(5)); // whichever fires first wins
try
{
return await _payerApi.CheckAsync(claimId, timeout.Token);
}
catch (OperationCanceledException) when (!ct.IsCancellationRequested)
{
throw new TimeoutException($"Payer API did not answer for claim {claimId} within 5 s");
}
}
// CPU-bound loops check the token themselves.
foreach (var row in rows)
{
ct.ThrowIfCancellationRequested();
Process(row);
}The exception filter (the when clause) separates our timeout from the caller cancelling. If the caller's token was cancelled, the exception passes through unchanged. Otherwise our timeout fired, and we report it as a timeout.
Running work concurrently
Awaits written one after another run one after another. To run independent calls at the same time, start all the tasks first, then await them together:
// Sequential: total time = patient + payer + history.
var patient = await _patients.GetAsync(patientId, ct);
var payer = await _payers.GetAsync(payerId, ct);
var history = await _claims.GetHistoryAsync(patientId, ct);// Concurrent: start all three, then await. Total time = the slowest of the three.
var patientTask = _patients.GetAsync(patientId, ct);
var payerTask = _payers.GetAsync(payerId, ct);
var historyTask = _claims.GetHistoryAsync(patientId, ct);
await Task.WhenAll(patientTask, payerTask, historyTask);
var patient = await patientTask; // already complete: returns at once, no extra wait
var payer = await payerTask;
var history = await historyTask;One trap: an EF Core DbContext cannot run two operations at the same time, so these three calls must not share one DbContext. Give each call its own (through IDbContextFactory), or keep database calls sequential and run only independent HTTP calls concurrently.
Starting thousands of tasks at once with WhenAll can overload the service you call or break its rate limit. Parallel.ForEachAsync (.NET 6+) processes a collection with a fixed maximum number of calls running at once:
var options = new ParallelOptions
{
MaxDegreeOfParallelism = 8, // at most 8 calls in flight, e.g. the payer API's limit
CancellationToken = ct
};
await Parallel.ForEachAsync(claimIds, options, async (claimId, token) =>
{
var result = await _payerApi.CheckAsync(claimId, token);
await _results.SaveAsync(claimId, result, token);
});Task.WhenAny returns the first task to finish. Use it for a race (take whichever replica answers first) or a timeout. The other tasks keep running, so cancel them once you have your answer.
Shared state: race conditions and lock
When two threads read, change and write the same variable, updates can be lost. count++ is three steps (read, add, write). Two threads can both read 41 and both write 42, so one increment is lost:
int unsafeCount = 0, safeCount = 0;
var gate = new object();
Parallel.For(0, 1_000_000, _ =>
{
unsafeCount++; // race: increments are lost
lock (gate) { safeCount++; } // one thread at a time inside the block
});
Console.WriteLine($"Unsafe: {unsafeCount:N0} Safe: {safeCount:N0}");The unsafe total changes on every run and is almost always below a million. The locked total is always exactly 1,000,000. lock (built on Monitor) lets only one thread into the block at a time; the others wait. Rules for using it:
- Keep locked sections short.
- Lock on a private object that no other code can reach (never this, a string or a Type).
- When you need several locks, always take them in the same order. Two threads taking locks A and B in opposite orders can deadlock.
.NET 9 adds a dedicated System.Threading.Lock type, which the lock statement uses directly and which is slightly faster.
You cannot await inside a lock block. The compiler rejects it, because the thread that resumes after the await may not be the thread that holds the lock. For a single number, Interlocked is cheaper than a lock and needs no lock object:
private long _processed;
public void OnClaimProcessed() => Interlocked.Increment(ref _processed);
// Compare-and-swap: only update if nobody changed the value since we read it.
private int _maxSeen;
public void Observe(int value)
{
int current;
do
{
current = Volatile.Read(ref _maxSeen);
if (value <= current) return;
} while (Interlocked.CompareExchange(ref _maxSeen, value, current) != current);
}SemaphoreSlim: an async-friendly lock and a throttle
SemaphoreSlim lets up to N callers in at once. Unlike lock, it has WaitAsync, which you can await. With a count of 1 it works as a lock you can await inside. With a larger count it limits how many operations run at once.
public sealed class TokenProvider(IIdentityClient identity)
{
private readonly SemaphoreSlim _refreshLock = new(1, 1);
private CachedToken? _token;
public async Task<string> GetTokenAsync(CancellationToken ct)
{
if (_token is { } t && t.ExpiresOn > DateTimeOffset.UtcNow.AddMinutes(1))
return t.Value; // fast path, no waiting
await _refreshLock.WaitAsync(ct);
try
{
// Check again: another caller may have refreshed while we waited.
if (_token is { } fresh && fresh.ExpiresOn > DateTimeOffset.UtcNow.AddMinutes(1))
return fresh.Value;
_token = await identity.RequestTokenAsync(ct); // await inside the critical section is fine
return _token.Value;
}
finally
{
_refreshLock.Release(); // always release, even on exceptions
}
}
}
public sealed record CachedToken(string Value, DateTimeOffset ExpiresOn);The second check inside the semaphore is the double-checked pattern. Without it, ten requests that all find an expired token would refresh it ten times. Always call Release in a finally block: a semaphore that is never released blocks every later caller. SemaphoreSlim only works inside one process. Across several instances of a service you need a distributed lock (Redis with an expiry, or a blob lease), covered in the lesson on API and NFR patterns.
| Tool | Use it for | Await inside? | Scope |
|---|---|---|---|
| lock / System.Threading.Lock | Short critical sections over in-memory state | No | One process |
| Interlocked | A single counter or reference, compare-and-swap | Not applicable | One process |
| SemaphoreSlim(1,1) | A critical section that awaits (token refresh, file write) | Yes | One process |
| SemaphoreSlim(N,N) | Limiting concurrent calls to a resource | Yes | One process |
| ReaderWriterLockSlim | Many readers, rare writers, synchronous code | No | One process |
| Distributed lock (Redis, blob lease) | One instance at a time across a cluster | Yes | Many processes |
Visibility: volatile and the memory model
Locks do two jobs. They let one thread in at a time, and they make sure each thread sees the writes made by the previous one. The second job matters even when threads do not collide. The compiler, the JIT and the CPU can reorder memory reads and writes, or keep a value in a CPU register, as long as a single thread cannot tell. Another thread can tell. The classic example is a stop flag that the worker never sees change:
public sealed class Poller
{
private bool _stop; // not volatile
private long _iterations;
public void Stop() => _stop = true; // called from another thread
public void Run()
{
// In an optimised Release build the JIT may read _stop once, keep it in a
// register and turn this into an infinite loop: Stop() is never seen.
while (!_stop) { _iterations++; }
}
}
// Fix: mark the field volatile (or use Volatile.Read / Volatile.Write at each access).
private volatile bool _stop;Marking a field volatile stops the compiler and CPU from reordering reads and writes around it, and makes every read fetch the latest value from memory. Volatile.Read and Volatile.Write give the same guarantee for a single access, which is why the compare-and-swap loop above uses Volatile.Read. But volatile does not make an update atomic (done as one indivisible step). _count++ on a volatile int is still read, add, write, and still loses updates. Use volatile for a flag or reference that one thread writes and others read. Use Interlocked or a lock when the new value depends on the old one. In application code, a CancellationToken is a better stop flag, because it already handles visibility.
| volatile / Volatile.Read | Interlocked | lock | |
|---|---|---|---|
| Every thread sees the latest write | Yes | Yes | Yes |
| Read-modify-write is atomic (count++) | No | Yes, for one variable | Yes, for the whole block |
| Protects several fields together | No | No | Yes |
| Cost | Lowest | Low | Higher, and waits under contention |
Per-thread and per-flow state: ThreadStatic, ThreadLocal and AsyncLocal
Sometimes the fix for shared state is not to share it: give each thread its own copy. .NET has three ways to do this, and picking the wrong one in async code causes real bugs.
[ThreadStatic] on a static field gives each thread its own value of that field. Its initialiser runs only once, on the first thread that uses the class, so every other thread sees the default value. Initialise it lazily on each thread instead. ThreadLocal<T> does the same with a factory that runs once per thread, and it is IDisposable. Both suit expensive objects that are not thread-safe, such as a reusable buffer: each thread gets its own, with no locking:
// [ThreadStatic]: one field per thread. Never give it an initialiser (= new ...):
// that would run on one thread only, and every other thread would see null.
[ThreadStatic] private static StringBuilder? t_buffer;
private static StringBuilder Buffer => t_buffer ??= new StringBuilder(capacity: 256);
public static string FormatRow(Claim claim)
{
var sb = Buffer;
sb.Clear();
sb.Append(claim.Id).Append(',').Append(claim.Amount);
return sb.ToString(); // no await between using the buffer and finishing with it
}
// ThreadLocal<T>: the factory runs once per thread that asks for .Value.
private static readonly ThreadLocal<SHA256> t_sha = new(() => SHA256.Create());
public static byte[] Hash(byte[] data) => t_sha.Value!.ComputeHash(data);Both break in async code. After an await, the method may resume on a different pool thread, which holds a different value, possibly one left over from another request, because pool threads are reused. Never keep per-request data (user, tenant, correlation ID) in [ThreadStatic] or ThreadLocal<T> in a web app.
AsyncLocal<T> is the async-safe option. Its value belongs to the logical flow of the code, not to a thread. It travels with the ExecutionContext (the context .NET carries across awaits) through awaits, Task.Run and thread switches. This is how Activity.Current (distributed tracing), IHttpContextAccessor and logging scopes keep per-request data available deep inside async calls:
public static class RequestContext
{
private static readonly AsyncLocal<string?> _correlationId = new();
[ThreadStatic] private static string? t_correlationId;
public static async Task HandleAsync(string id)
{
_correlationId.Value = id;
t_correlationId = id;
await Task.Delay(50); // may resume on another pool thread
Console.WriteLine($"AsyncLocal: {_correlationId.Value}"); // always the right id
Console.WriteLine($"ThreadStatic: {t_correlationId ?? "(null)"}"); // whatever this thread holds
}
}
await Task.WhenAll(RequestContext.HandleAsync("req-1"), RequestContext.HandleAsync("req-2"));AsyncLocal: req-1 ThreadStatic: (null) AsyncLocal: req-2 ThreadStatic: (null) (the ThreadStatic lines vary: null, the right id or the other request's id)
One rule surprises people: a value set inside an awaited child method does not flow back to the caller. The context is copied on the way in, so the child's changes stay in the child. Set AsyncLocal values at the start of the flow (in middleware or at the start of a message handler) and only read them further down.
| [ThreadStatic] | ThreadLocal<T> | AsyncLocal<T> | |
|---|---|---|---|
| Value belongs to | A thread | A thread | A logical async flow |
| Survives an await | No | No | Yes |
| Initialisation | Default value; initialise lazily per thread | Factory runs once per thread | Set by the flow; children inherit a copy |
| Use it for | Cheap per-thread caches and buffers in synchronous code | Per-thread instances of non-thread-safe objects | Correlation IDs, tenant, tracing context per request |
Signalling and the other primitives
Locks keep threads out of each other's way. Signalling primitives let one thread tell others that something has happened. Most come in an older blocking form and a newer form that is lighter or awaitable. Prefer the newer one.
TaskCompletionSource<T> is the async signal. It gives you a Task that you complete yourself, which turns any callback or event into something you can await. Create it with TaskCreationOptions.RunContinuationsAsynchronously. Otherwise the code awaiting the task runs on the thread that calls SetResult, which can cause unexpected re-entry and deadlocks:
public sealed class ReadySignal
{
private readonly TaskCompletionSource _ready =
new(TaskCreationOptions.RunContinuationsAsynchronously);
public Task WaitAsync(CancellationToken ct) => _ready.Task.WaitAsync(ct); // .NET 6+
public void SetReady() => _ready.TrySetResult(); // safe to call twice
}
// Example: a hosted service warms a cache at startup; requests wait until it is ready.
await _signal.WaitAsync(ct);A Mutex is a lock owned by the operating system, so a named Mutex works across processes. For example, it can make sure only one copy of a scheduled importer runs on a machine. Only the thread that acquired it can release it, so never hold one across an await. It is also much slower than lock. Across machines, you need a distributed lock instead.
using var mutex = new Mutex(initiallyOwned: false, name: @"Global\ClaimsImporter");
if (!mutex.WaitOne(TimeSpan.Zero))
{
Console.WriteLine("Another importer is already running on this machine.");
return;
}
try { RunImport(); } // synchronous work only: no await while holding a Mutex
finally { mutex.ReleaseMutex(); }EventWaitHandle is the operating system event behind AutoResetEvent and ManualResetEvent; those two are EventWaitHandle with the reset mode fixed. Used directly, you choose the mode. With EventResetMode.AutoReset, Set releases one waiter and the event closes again. With EventResetMode.ManualReset, Set releases every waiter and the event stays open until Reset. Because it is an operating system object, you can name it and share it between processes, like a named Mutex. It is also a WaitHandle, so WaitHandle.WaitAny and WaitAll can wait on several handles at once. The cost is a call into the operating system on every Set and Wait, so inside one process prefer ManualResetEventSlim or TaskCompletionSource.
const string Name = @"Global\ClaimsFileLoaded";
// Process B, the importer: creates the event (or opens it if A got there first) and signals it.
using var loaded = new EventWaitHandle(initialState: false, EventResetMode.ManualReset, Name, out bool createdNew);
ImportTodaysFile();
loaded.Set(); // every process waiting on this name is released
// Process A, the report job: waits for the importer, but gives up after 30 minutes or on shutdown.
using var fileLoaded = new EventWaitHandle(false, EventResetMode.ManualReset, Name);
using var shutdown = new ManualResetEvent(false); // set by this process's shutdown hook
int which = WaitHandle.WaitAny(new WaitHandle[] { fileLoaded, shutdown }, TimeSpan.FromMinutes(30));
switch (which)
{
case 0: GenerateReport(); break; // the file is loaded
case 1: return; // we are shutting down
case WaitHandle.WaitTimeout: throw new TimeoutException("Importer did not finish in 30 minutes");
}Two cautions:
- Named events other than Mutex only work on Windows. On Linux and macOS, creating a named EventWaitHandle throws PlatformNotSupportedException, so cross-process signalling in containers needs something else (a file lock, a database row or a message).
- WaitOne blocks the thread. In async code, wrap the handle in a TaskCompletionSource using ThreadPool.RegisterWaitForSingleObject instead of blocking a pool thread.
| Primitive | What it does | Typical use |
|---|---|---|
| TaskCompletionSource<T> | A Task you complete yourself; awaitable | Wrapping callbacks or events, one-off ready signals |
| ManualResetEventSlim | A gate: Set opens it for every waiter until Reset | Synchronous code waiting for initialisation |
| AutoResetEvent | Lets exactly one waiter through per Set | Legacy hand-off between two threads (prefer Channels) |
| ManualResetEvent | Kernel version of the gate; a WaitHandle | WaitAny / WaitAll together with other handles |
| EventWaitHandle (named) | An OS event, auto or manual reset, shared across processes | Signalling between processes on Windows |
| CountdownEvent | Opens once N signals have arrived | Waiting for N synchronous workers (prefer Task.WhenAll) |
| Barrier | N threads wait for each other at the end of each phase | Phased parallel algorithms and simulations |
| Monitor.Wait / Pulse | Wait and wake inside a lock | Hand-written blocking queues (prefer Channels) |
| Mutex (named) | An OS lock shared across processes | One instance of a process per machine |
| SpinLock / SpinWait | Busy-waits instead of sleeping | Extremely short sections in low-level libraries only |
Concurrent collections
Dictionary<TKey,TValue> and List<T> are not safe for writes from several threads. A Dictionary written by two threads at once can corrupt itself and even loop forever on a later read. System.Collections.Concurrent has collections built for many threads: ConcurrentDictionary, ConcurrentQueue, ConcurrentStack, ConcurrentBag and BlockingCollection.
ConcurrentDictionary has a trap that catches many experienced developers. GetOrAdd(key, factory) is thread-safe for the dictionary, but not for the factory. If two threads miss the same key at the same moment, both run the factory, and one result is thrown away. When the factory is expensive or has side effects (an HTTP call, opening a connection), store a Lazy<T> so it runs only once:
// _configs.GetOrAdd(code, c => _configApi.LoadAsync(c)) could call LoadAsync twice for one key.
// Lazy<T> guarantees the expensive part runs once per key.
private readonly ConcurrentDictionary<string, Lazy<Task<PayerConfig>>> _configs = new();
public Task<PayerConfig> GetConfigAsync(string payerCode) =>
_configs.GetOrAdd(payerCode,
code => new Lazy<Task<PayerConfig>>(() => _configApi.LoadAsync(code))).Value;For combined operations, use the atomic methods: AddOrUpdate, GetOrAdd, TryUpdate and TryRemove. Calling ContainsKey and then Add is a race, because another thread can add the key between the two calls.
Channels: producer/consumer pipelines
System.Threading.Channels is the modern way to pass work from producers to consumers inside one process. A channel is an async queue: writers await WriteAsync, readers await ReadAsync or ReadAllAsync, and no thread is blocked. A bounded channel adds backpressure: when it is full, writers wait instead of using more and more memory. Channels replace BlockingCollection, which blocks threads while waiting.
// Registered once: a bounded queue between the API and a background worker.
builder.Services.AddSingleton(Channel.CreateBounded<ClaimJob>(new BoundedChannelOptions(500)
{
FullMode = BoundedChannelFullMode.Wait, // writers wait when 500 jobs are queued (backpressure)
SingleReader = true
}));
builder.Services.AddHostedService<ClaimWorker>();
// Producer: the API accepts the request and returns 202 straight away.
app.MapPost("/claims/{id}/recheck", async (int id, Channel<ClaimJob> channel, CancellationToken ct) =>
{
await channel.Writer.WriteAsync(new ClaimJob(id), ct);
return Results.Accepted($"/claims/{id}/status");
});
// Consumer: a BackgroundService reads jobs as they arrive.
public sealed class ClaimWorker(Channel<ClaimJob> channel,
IServiceScopeFactory scopes,
ILogger<ClaimWorker> log) : BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
await foreach (var job in channel.Reader.ReadAllAsync(stoppingToken))
{
try
{
using var scope = scopes.CreateScope(); // a fresh DbContext per job
var service = scope.ServiceProvider.GetRequiredService<IClaimService>();
await service.RecheckAsync(job.ClaimId, stoppingToken);
}
catch (Exception ex) when (ex is not OperationCanceledException)
{
log.LogError(ex, "Recheck failed for claim {ClaimId}", job.ClaimId); // one bad job doesn't stop the worker
}
}
}
}
public sealed record ClaimJob(int ClaimId);Two details matter. The worker is a Singleton, so it creates a new scope for each job instead of holding a Scoped DbContext (which would make the DbContext a captive dependency that lives as long as the app). It also catches exceptions per job, because an exception that escapes ExecuteAsync stops the hosted service. An in-memory channel loses its queued jobs when the process restarts. When jobs must survive a restart or be shared across instances, use a durable queue such as Azure Service Bus; the producer/consumer code looks the same.
Thread pool starvation
Thread pool starvation happens when pool threads are blocked faster than the pool can add new ones. The pool starts with about one thread per CPU core and adds more slowly when all are busy. If requests block threads with .Result, .Wait() or Thread.Sleep while waiting for I/O, queued work waits for a free thread, including the continuations that would unblock them. Response times climb to seconds while CPU stays low. That is the sign: the service is slow but not busy.
# Watch the runtime counters of a running process (dotnet tool install -g dotnet-counters)
dotnet-counters monitor --process-id 12345 --counters System.Runtime
# Signs of starvation:
# ThreadPool Queue Length keeps growing
# ThreadPool Thread Count climbs slowly and steadily
# CPU Usage stays low while response times riseThe fix is to remove the blocking:
- Make the call chain async all the way.
- Replace Thread.Sleep with await Task.Delay.
- Replace .Result and .Wait() (sync-over-async) with await.
Raising the minimum thread count with ThreadPool.SetMinThreads only hides the problem until load grows. To find the blocking calls, take a memory dump or trace (dotnet-dump, dotnet-trace) while it is happening and look for many threads stuck in Task.Wait or Monitor.Wait.
Check your understanding
| Question | Short answer |
|---|---|
| Does async make code faster? | No. It frees threads while waiting for I/O, so a server handles more requests with the same threads. CPU-bound work is not faster. |
| What happens at an await? | If the task is incomplete, the method registers the rest of itself as a continuation and returns an incomplete Task; the thread goes back to the pool. It resumes when the task completes. |
| Why can .Result deadlock? | On a thread with a single-threaded SynchronizationContext, the blocked thread waits for the task, while the task's continuation waits for that thread. ASP.NET Core has no such context, but .Result still wastes a thread and can starve the pool. |
| What does ConfigureAwait(false) do? | Resumes on the thread pool instead of the captured context. Use it in library code; it changes nothing in ASP.NET Core application code. |
| Why avoid async void? | The caller cannot await it or catch its exceptions, and an unhandled exception crashes the process. Only use it for event handlers. |
| Task or ValueTask? | Task by default. ValueTask on hot paths that usually complete synchronously; await it once only. |
| Task.Run in ASP.NET Core? | Not for I/O: it swaps one pool thread for another. Only for CPU-bound work you deliberately run in parallel. |
| How do you limit concurrency? | Parallel.ForEachAsync with MaxDegreeOfParallelism, or SemaphoreSlim(N) around the call. |
| Why can't you await inside lock? | The thread that resumes may not own the lock. Use SemaphoreSlim(1,1) with WaitAsync and Release in finally. |
| volatile or lock? | volatile only guarantees visibility for one field; it does not make count++ atomic. Use Interlocked for one value, lock for anything compound. |
| Why is [ThreadStatic] dangerous in async code? | After an await the method may resume on another thread, which holds a different value, and pool threads are reused across requests. Use AsyncLocal<T> for per-request data. |
| EventWaitHandle or ManualResetEventSlim? | EventWaitHandle is a kernel event: it can be named and shared across processes and used with WaitAny, but every call goes to the OS. Inside one process use the Slim version or TaskCompletionSource. |
| How do you await a callback or event? | Wrap it in a TaskCompletionSource created with RunContinuationsAsynchronously. |
| What is the ConcurrentDictionary.GetOrAdd trap? | The factory can run more than once for the same key under contention. Store Lazy<T> when it is expensive. |
| Channels or BlockingCollection? | Channels: fully async, with bounded backpressure, so no threads are blocked waiting. |
| How do you spot thread pool starvation? | Latency rises while CPU stays low; the thread pool queue length grows. Fix the blocking calls rather than raising the minimum thread count. |
Summary
- Use async/await for I/O so threads go back to the pool while waiting, and stay async all the way instead of blocking with .Result or .Wait().
- Return Task by default, ValueTask only on measured hot paths, and never async void outside event handlers.
- Pass a CancellationToken through every async call.
- Run independent calls together with Task.WhenAll, and limit how many run at once with Parallel.ForEachAsync or SemaphoreSlim.
- Protect shared state with the lightest tool that works: volatile for a flag one thread writes, Interlocked for one value, lock for short synchronous sections, SemaphoreSlim when you need to await, and the atomic methods of the concurrent collections.
- Keep per-request data in AsyncLocal<T>, never in [ThreadStatic] or ThreadLocal<T>, and use TaskCompletionSource to await signals.
- Use Channels for in-process producer/consumer work, and a durable queue when the work must survive a restart.
The next lesson goes into memory: how the garbage collector works, the Large Object Heap, IDisposable, Span<T> and ArrayPool, and how to measure allocations.
Comments