Skip to content

Concurrency and Parallelism in C# — A Practical Guide

Understand concurrency vs parallelism in C#: async I/O, Task.WhenAll and WhenEach, CPU parallelism, PLINQ, SIMD, throttling, channels, shared state, cancellation, and production mistakes.

Isaiah Clifford Opoku
Isaiah Clifford OpokuSoftware Engineer & ArchitectNovember 19, 2026·35 min read

Your dashboard calls three services. Each takes about 300 ms, so calling them one after another makes the user wait roughly 900 ms. Meanwhile, your nightly image job has a different problem: one CPU core is busy resizing images while the others have capacity to help.

Both jobs could benefit from overlapping work, but they are slow for different reasons. The dashboard spends much of its time waiting for responses. The image job spends much of its time performing calculations. Choosing the same solution for both is where concurrency code becomes confusing.

Let’s follow those two jobs through the article. First we’ll make the dashboard’s independent calls overlap using asynchronous I/O. Then we’ll split computation across workers so available cores can work simultaneously. With that distinction established, we’ll look at limiting the work, queuing it, and keeping its state safe.

Examples target .NET 10 / C# 14. Application services such as _api, _users, and _reportEngine are illustrative dependencies. The snippets are separate examples rather than one application to paste into a single file. Version requirements are noted alongside newer APIs.

Concurrency overlaps operations; parallelism executes work simultaneously

Concurrency asks: can another operation make progress before this one finishes? Our dashboard can start the orders request while the user request is still waiting for a response. Both requests are in progress, even if the local CPU has little to do during those waits.

Parallelism asks: can multiple pieces of work execute at the same instant? Our image job can give independent images to different workers so available CPU cores perform calculations simultaneously.

Parallel work is also concurrent: those operations have overlapping lifetimes. But concurrent operations do not have to execute simultaneously. One core can switch between operations, and asynchronous I/O can leave it free to do something else while a response is pending.

One chef who puts pasta on to boil, chops vegetables while it cooks, and checks the sauce is working concurrently. Four chefs cooking separate dishes at the same time are working in parallel.

gantt
    title One core, concurrent work interleaved
    dateFormat x
    axisFormat %L
    section Core 1
    Task A :a1, 0, 200ms
    Task B :b1, after a1, 200ms
    Task A :a2, after b1, 200ms
    Task C :c1, after a2, 200ms
    Task B :b2, after c1, 200ms
gantt
    title Three cores, parallel work
    dateFormat x
    axisFormat %L
    section Core 1
    Task A :a, 0, 600ms
    section Core 2
    Task B :b, 0, 600ms
    section Core 3
    Task C :c, 0, 600ms

The first timeline shows one core switching between operations. The second shows work executing on three cores at once. These are scheduling illustrations, not performance measurements.

For our two jobs, that distinction leads to two paths:

flowchart TD
    C["Concurrency: multiple operations in progress"] --> A["Asynchronous I/O"]
    C --> P["Parallel computation"]
    A --> A1["HTTP, database, file operations"]
    A --> A2["Avoid blocking threads while waiting"]
    P --> P1["Math, image transforms, parsing"]
    P --> P2["Use available compute capacity"]
Kind of work Main limitation Typical tools Common mistake
I/O-bound Waiting on network, storage, database async/await, Task.WhenAll Blocking a pool thread, then wrapping it in Task.Run
CPU-bound Computation Parallel.For/ForEach, PLINQ, SIMD Expecting async alone to make math faster

If your code spends its time waiting, start with asynchrony. If it spends its time computing, consider parallelism and measure it.

Keep the practical difference in mind as we move into C#: overlapping waits saves waiting time; parallel calculation uses more compute capacity. Neither removes limits such as database capacity, memory, or the number of available cores.

What a Task represents, and where threads fit

Before changing either job, we need to separate two terms that appear in both examples: a task and a thread.

A thread is an execution resource the operating system schedules. Threads need stacks and OS bookkeeping; their memory cost depends on platform and configuration. Creating one per request is expensive, so .NET reuses worker threads through the thread pool.

A Task is not a thread. It represents an operation’s eventual completion. CPU work may occupy a pool thread, while an asynchronous network operation need not occupy a worker for its entire wait.

flowchart LR
    subgraph Code[CPU work queued by your code]
        T1[Task 1]
        T2[Task 2]
        T3[Task 3]
        T4[Task 4]
    end
    subgraph Pool[Thread pool]
        Q[(Work queues)]
        W1[Worker 1]
        W2[Worker 2]
        W3[Worker 3]
    end
    T1 & T2 & T3 & T4 --> Q
    Q --> W1 & W2 & W3
    W1 & W2 & W3 --> CPU[Available CPU cores]

The diagram is for queued CPU work, not every task in the system. The pool creates workers on demand and adapts its worker count. Its minimum-worker setting is not a promise that all those threads exist at startup. Modern runtimes compensate for some blocking patterns faster than older versions, but blocking can still cause starvation and expensive thread growth.

You’ll rarely need to construct a Thread directly:

// Dedicated thread: your code owns its lifetime.
var thread = new Thread(() => DoWork());
thread.Start();
thread.Join();

// Queued work: the runtime manages reusable workers.
await Task.Run(() => DoWork());

The distinction matters in the next example: three tasks representing HTTP calls do not mean three worker threads must sit waiting. Later, the CPU example will need workers because the calculations themselves must execute somewhere.

When the job is waiting: overlap I/O with async and await

Return to the dashboard. It needs a user profile, recent orders, and alerts from independent services. While a service prepares its response, our application has no useful calculation to perform for that call. This is I/O-bound work.

Our first goal is to avoid blocking a thread during that wait. Once we understand that, we can overlap the three independent calls.

What await changes about waiting

If an awaited operation has not completed, the async method records how to continue and returns control to its caller. With genuinely asynchronous I/O, a worker need not sit blocked waiting for the response. If the task is already complete, execution can continue synchronously.

sequenceDiagram
    participant R as Request handler
    participant DB as Database
    participant P as Thread pool
    R->>DB: Start async query
    Note over R: Await incomplete operation, suspend method
    R-->>P: Worker becomes available for other work
    DB-->>P: I/O completes
    P->>R: Run continuation on an available worker
    R->>R: Build response

That is the typical ASP.NET Core model. A desktop app may instead resume on its UI synchronization context. await does not inherently create a new thread, and adding async to a blocking implementation does not make it nonblocking.

// Blocking: keeps a thread occupied while waiting.
public Order GetOrder(int id)
    => _db.Orders.First(o => o.Id == id);

// Async: lets the provider perform asynchronous I/O.
public async Task<Order> GetOrderAsync(int id, CancellationToken ct)
    => await _db.Orders.FirstAsync(o => o.Id == id, ct);

Make the dashboard calls overlap

Making each call async frees the worker during its wait, but it does not automatically overlap the calls. In this version, we still wait for the user response before starting the orders request, then wait again before requesting alerts:

public async Task<Dashboard> GetDashboardAsync(int userId, CancellationToken ct)
{
    var user = await _users.GetAsync(userId, ct);          // ~300 ms
    var orders = await _orders.GetRecentAsync(userId, ct); // ~300 ms
    var alerts = await _alerts.GetAsync(userId, ct);       // ~300 ms

    return new Dashboard(user, orders, alerts);           // ~900 ms
}

So the endpoint is asynchronous, yet the user’s total wait is still around 900 ms. Because these three operations are independent, we can start each one before awaiting their combined completion:

public async Task<Dashboard> GetDashboardAsync(int userId, CancellationToken ct)
{
    var userTask = _users.GetAsync(userId, ct);
    var ordersTask = _orders.GetRecentAsync(userId, ct);
    var alertsTask = _alerts.GetAsync(userId, ct);

    await Task.WhenAll(userTask, ordersTask, alertsTask);

    return new Dashboard(await userTask, await ordersTask, await alertsTask);
}

After WhenAll completes successfully, awaiting each task returns its existing result. Reading .Result at that point would not block either, but using await consistently avoids normalizing blocking access elsewhere.

gantt
    title Illustrative service latency in milliseconds
    dateFormat x
    axisFormat %L
    section Sequential
    Users  :s1, 0, 300ms
    Orders :s2, after s1, 300ms
    Alerts :s3, after s2, 300ms
    section Concurrent
    Users  :u, 0, 300ms
    Orders :o, 0, 300ms
    Alerts :a, 0, 300ms

The ideal concurrent duration is around the longest call, plus overhead. This overlaps I/O waits; it does not require running the remote operations on separate local CPU cores. Real latency depends on connection pools, service limits, and contention.

EF Core warning: one DbContext does not support multiple concurrent operations. If these services share a context, await those queries sequentially or create separate contexts, for example through IDbContextFactory<T>. Separate contexts also mean separate units of work; decide whether that meets your consistency requirements.

The change is in when the calls start. We didn’t ask three local cores to calculate the responses faster. We stopped making one independent network wait follow another. That is the concurrency benefit of this example.

If the orders call needed a value returned by the user call, that dependency would still have to wait. Only overlap work that can safely proceed independently.

When the job is computing: run independent CPU work in parallel

Now leave the dashboard and look at the image job. Once an image is in memory, resizing it requires calculations. There is no remote response to wait for during that computation. Adding async or awaiting the result does not make those calculations finish sooner.

If images can be processed independently, we can give different images to different workers. With available cores, those workers can execute calculations simultaneously. This is CPU parallelism: the work itself runs at the same instant.

Loading and saving images also involves I/O, so the complete job is mixed. We’re focusing here on its compute-heavy stage.

flowchart LR
    D[(10,000 images)] --> S[Partition work]
    S --> C1[Worker 1]
    S --> C2[Worker 2]
    S --> C3[Worker 3]
    S --> C4[Worker 4]
    C1 & C2 & C3 & C4 --> R[Combine completion]

See parallel computation with two independent sums

Before the image loop, take a smaller example: adding a large range of numbers. Split the range into two independent halves and queue each computation with Task.Run:

using System.Diagnostics;

var stopwatch = Stopwatch.StartNew();

Task<long> task1 = Task.Run(() => SumRange(0, 500_000_000));
Task<long> task2 = Task.Run(() => SumRange(500_000_000, 1_000_000_000));

long[] results = await Task.WhenAll(task1, task2);

Console.WriteLine($"Total: {results.Sum()} in {stopwatch.ElapsedMilliseconds} ms");

static long SumRange(long from, long to)
{
    long sum = 0;
    for (long i = from; i < to; i++) sum += i;
    return sum;
}
sequenceDiagram
    participant M as Main method
    participant P as Thread pool
    participant W1 as Worker 1
    participant W2 as Worker 2
    M->>P: Queue first half with Task.Run
    M->>P: Queue second half with Task.Run
    P->>W1: Execute first half
    P->>W2: Execute second half
    Note over M: Await the combined task
    W1-->>M: Result 1
    W2-->>M: Result 2
    Note over M: Both complete, print total

Task.Run queues work to the thread pool. Available workers execute it; with enough compute capacity, the computations may run in parallel. Task.WhenAll creates a task representing completion of both. It does not itself start work or guarantee two dedicated threads.

This may be faster than running both halves sequentially, but a two-times speedup is not guaranteed. Scheduling, CPU quotas, other traffic, JIT compilation, and the workload affect the result. Benchmark a release build against a sequential baseline.

Notice that await Task.WhenAll appears in both the dashboard and this example. It waits for completion in both cases. The distinction comes from the work we started: the dashboard started asynchronous I/O; here, Task.Run queued calculations for workers to execute.

Spread the image batch across workers

Manually creating one task per image would leave us managing a large batch ourselves. Parallel.ForEach handles partitioning and scheduling loop iterations. A small loop shows how the API is shaped:

Parallel.ForEach(Enumerable.Range(1, 10), item =>
{
    Console.WriteLine($"Item {item}, thread {Environment.CurrentManagedThreadId}");
});

Order and exact thread assignments are not guaranteed. Depending on workload and scheduling, a small loop may even use only one worker. Printing demonstrates scheduling, not a useful performance benchmark.

Here’s thumbnail generation using the SixLabors.ImageSharp package:

using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;

Directory.CreateDirectory("thumbs");
var files = Directory.GetFiles("images", "*.jpg");
var options = new ParallelOptions
{
    MaxDegreeOfParallelism = Math.Max(1, Environment.ProcessorCount - 1),
    CancellationToken = cancellationToken
};

Parallel.ForEach(files, options, file =>
{
    using var image = Image.Load(file);
    image.Mutate(x => x.Resize(800, 600));
    image.Save(Path.Combine("thumbs", Path.GetFileName(file)));
});

Check ImageSharp’s license for your use case. Image dimensions, memory per image, disk throughput, and the library’s own internal parallelism all affect a sensible limit.

Parallel.ForEach blocks its caller until the loop completes or fails, and the caller may participate in execution. MaxDegreeOfParallelism caps concurrent loop work; it does not reserve cores or guarantee one stays free. Math.Max(1, ...) keeps the limit valid on a single-processor environment.

Independent array slots are useful for parallel work:

var results = new double[1_000_000];
Parallel.For(0, results.Length, options, i =>
{
    results[i] = ExpensiveCalculation(i);
});

Each iteration writes its own slot. That doesn’t make ExpensiveCalculation safe if it mutates other shared data, and adjacent writes can still suffer cache contention.

Decide whether the extra workers are helping

The image job can benefit when enough independent computation is available. But using several workers has costs: partitioning, scheduling, synchronization, memory bandwidth, and combining results. If each iteration is very cheap, those costs can outweigh the work:

// Cheap iterations: a sequential loop may be faster.
Parallel.For(0, 1000, i => numbers[i] *= 2);

Parallelize substantial independent work, and benchmark before and after. More items alone do not guarantee a win if each iteration is trivial.

Speedup also depends on the serial portion. If 20% of execution remains sequential, the theoretical limit is 5x even with unlimited parallel capacity. Under server load, parallelizing each request can reduce overall throughput by making requests compete for the same cores.

Compare what changed in the two jobs

We used overlapping work in both examples, but the source of the improvement was different:

Question Dashboard service calls Image calculations
What is holding up progress? Waiting for external responses Performing calculations
What did we change? Started independent calls before awaiting them Assigned independent work to multiple workers
Why can it finish sooner? The waits overlap Available cores compute simultaneously
Does it need multiple local cores for that benefit? No, overlapping I/O waits does not require them Yes, for the multicore execution described here
What is the main limit? Downstream capacity and connection limits Available CPU capacity, memory, and scheduling costs

A real request can contain both stages. It might download several images asynchronously, then resize them with a bounded amount of CPU parallelism. Choose the approach for each stage rather than calling the whole request either “async” or “parallel.”

Now that we know why work should overlap, the next question is how much to allow at once.

Keep overlapping work within a sensible limit

Three dashboard calls are manageable. But suppose the same application must call an API for 5,000 customers. Starting every call at once could overwhelm the service or exhaust connections.

This is still I/O-bound work. We want overlapping waits, with a limit on how many operations are active. Parallel.ForEachAsync gives us an asynchronous loop with that limit; despite its name, using it for HTTP calls does not mean each call needs its own CPU core.

// No application concurrency limit: enumeration initiates all the calls.
var tasks = customerIds.Select(id => _api.SyncCustomerAsync(id));
await Task.WhenAll(tasks);

Instead, set a deliberate concurrency limit:

var options = new ParallelOptions
{
    MaxDegreeOfParallelism = 10,
    CancellationToken = cancellationToken
};

await Parallel.ForEachAsync(customerIds, options, async (id, ct) =>
{
    await _api.SyncCustomerAsync(id, ct);
});

The default for ForEachAsync is based on Environment.ProcessorCount. For I/O, explicitly choose a limit based on downstream capacity, connections, and measurements.

flowchart LR
    Q[5,000 customer IDs] --> G[At most 10 iterations active]
    G --> S1[Request 1]
    G --> S2[Request 2]
    G --> S3[Other active requests]
    S1 & S2 & S3 --> API[(External API)]
    API --> N[Completion frees an iteration slot]
    N --> G

A concurrency limit is not a rate limit. Ten fast operations can still exceed a quota of ten requests per second. Use an actual rate limiter when the API specifies a time-based quota, and respect retry guidance.

SemaphoreSlim: the manual throttle

The loop’s limit covers that one loop invocation. Suppose instead several endpoints can all call the same report engine. We need those callers to share a limit, even though they aren’t in one loop. A shared SemaphoreSlim can control admission across those calls:

public class ReportService
{
    private static readonly SemaphoreSlim _gate = new(3, 3);

    public async Task<Report> GenerateAsync(int id, CancellationToken ct)
    {
        await _gate.WaitAsync(ct);
        try
        {
            return await _reportEngine.RunAsync(id, ct);
        }
        finally
        {
            _gate.Release();
        }
    }
}

Waiting happens before try: if acquisition is canceled, no slot was acquired and none should be released. After successful acquisition, finally returns it even when the operation fails.

This static semaphore limits this process, not every replica of your deployed application. Five server instances each allowing three operations can allow fifteen altogether. A global limit needs coordination outside one process.

Queue work when it arrives faster than it can be processed

A limit tells us how many operations can proceed. It doesn’t give us a place to hand work from one part of the application to another.

Imagine uploads arriving throughout the day. The endpoint receives each file, while background workers process saved files at their own pace. A queue separates those responsibilities. System.Threading.Channels provides an asynchronous queue for this producer/consumer pattern.

The channel coordinates the handoff. Whether processing is asynchronous I/O or CPU computation still depends on what the consumers do.

flowchart LR
    P1[API endpoint] -->|WriteAsync| CH[Bounded channel: 100 jobs]
    P2[File watcher] -->|WriteAsync| CH
    CH -->|ReadAllAsync| C1[Consumer 1]
    CH -->|ReadAllAsync| C2[Consumer 2]
    CH -->|ReadAllAsync| C3[Consumer 3]
    C1 & C2 & C3 --> S[Process jobs]

Here is the queue and worker structure for an ASP.NET Core image pipeline. SaveToDiskAsync and ProcessImageAsync represent application-specific implementations.

using System.Threading.Channels;

public record ImageJob(string Path, int UserId);

public class ImageQueue
{
    private readonly Channel<ImageJob> _channel = Channel.CreateBounded<ImageJob>(
        new BoundedChannelOptions(100)
        {
            FullMode = BoundedChannelFullMode.Wait
        });

    public ValueTask EnqueueAsync(ImageJob job, CancellationToken ct = default)
        => _channel.Writer.WriteAsync(job, ct);

    public IAsyncEnumerable<ImageJob> ReadAllAsync(CancellationToken ct)
        => _channel.Reader.ReadAllAsync(ct);

    public bool Complete() => _channel.Writer.TryComplete();
}
public class ImageProcessor(ImageQueue queue, ILogger<ImageProcessor> logger)
    : BackgroundService
{
    protected override Task ExecuteAsync(CancellationToken stoppingToken)
    {
        var workers = Enumerable.Range(1, 3)
            .Select(n => RunWorkerAsync(n, stoppingToken));

        return Task.WhenAll(workers);
    }

    private async Task RunWorkerAsync(int workerId, CancellationToken ct)
    {
        await foreach (var job in queue.ReadAllAsync(ct))
        {
            try
            {
                await ProcessImageAsync(job, ct);
                logger.LogInformation("Worker {Id} processed {Path}", workerId, job.Path);
            }
            catch (OperationCanceledException) when (ct.IsCancellationRequested)
            {
                throw;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "Failed to process {Path}", job.Path);
            }
        }
    }
}

The processing implementation must really yield during asynchronous work if you expect these consumers to overlap. If it performs only synchronous CPU work, creating three async methods doesn’t itself put that work on three threads. For CPU transforms, add a deliberately bounded compute stage or use dedicated workers with an explicit scheduling policy.

builder.Services.AddSingleton<ImageQueue>();
builder.Services.AddHostedService<ImageProcessor>();

app.MapPost("/images", async (
    IFormFile file, ImageQueue queue, CancellationToken ct) =>
{
    var path = await SaveToDiskAsync(file, ct);
    try
    {
        // Demo ID: replace with the authenticated user's identifier.
        await queue.EnqueueAsync(new ImageJob(path, UserId: 1), ct);
    }
    catch
    {
        File.Delete(path); // Clean up a saved upload that was not queued.
        throw;
    }
    return Results.Accepted();
});

For production uploads, validate size and content, generate server-controlled filenames, configure the endpoint’s antiforgery/authentication behavior, and define file cleanup and retry policies. A hosted service is singleton-lived; resolve scoped dependencies such as DbContext from a fresh scope per job rather than sharing one across workers. The earlier hosted services guide covers those lifetimes.

A bounded channel applies backpressure when its buffer fills:

Full mode Behavior when full
Wait WriteAsync waits for room
DropOldest Removes the oldest buffered item to admit the new item
DropNewest Removes the newest buffered item to admit the new item
DropWrite Drops the incoming item

The capacity bounds buffered items, not all memory used by waiting producers or uploaded files. Admission limits, request timeouts, and cleanup still matter. Multiple consumers distribute items; a channel is not a broadcast to every consumer, and completion order can differ from enqueue order.

In-memory channels are not durable. This worker logs failed jobs rather than retrying them, and cancels during shutdown rather than guaranteeing a drain. A graceful-drain design must stop producers, complete the writer, and allow consumers to finish within a bounded shutdown period. For jobs that must survive restarts, use a durable broker or job store and explicitly define retry and dead-letter behavior.

Keep results correct when operations share data

Our calls can now overlap, and our workers can process queued jobs. Suppose every worker also increments a shared “images processed” counter. Finishing the image work correctly doesn’t guarantee that shared counter is correct.

When multiple threads access mutable data and at least one writes without suitable coordination, results can depend on timing. This is a correctness problem for concurrent work, especially when workers execute in parallel. Let’s follow the counter first, then look at operations that need more than one protected update.

Why count++ isn’t safe

An increment is a read-modify-write operation. Two threads can both read the same value and overwrite each other’s increment:

sequenceDiagram
    participant A as Thread A
    participant M as Shared count
    participant B as Thread B
    Note over M: count starts at 5
    A->>M: Read 5
    B->>M: Read 5
    A->>A: Calculate 6
    B->>B: Calculate 6
    A->>M: Write 6
    B->>M: Write 6
    Note over M: Actual 6, intended 7
int count = 0;
Parallel.For(0, 100_000, _ => count++);
Console.WriteLine(count); // May be less than 100,000; sometimes looks correct.

A correct result on one run does not prove the code is safe. We need to make the update safe regardless of how the threads are scheduled. For this counter, that means making the whole increment one atomic operation.

Fixing the counter with Interlocked

We don’t need to stop every worker and protect a whole block of code just to increment one number. Interlocked.Increment performs the read-modify-write atomically, so another thread cannot slip an increment between its steps:

int count = 0;
Parallel.For(0, 100_000, _ => Interlocked.Increment(ref count));
Console.WriteLine(count); // 100,000 after the loop completes.

Now every completed iteration contributes exactly one increment. Interlocked also provides atomic operations such as Add, Exchange, and CompareExchange. These avoid an explicit monitor lock, but many threads hammering the same value still contend for shared memory.

That solves our counter. But suppose the operation is a withdrawal: you must check the balance, subtract the amount, and record the withdrawal. Making one of those steps atomic does not protect the rule that ties them together.

Keeping a whole operation consistent with lock

Imagine two withdrawals both reading the same balance and deciding there is enough money. If we protect only the subtraction, both can still pass the earlier check. We need one caller at a time to perform the check and the updates together.

That’s what lock gives us. Since .NET 9 / C# 13, the compiler recognizes System.Threading.Lock:

public class BankAccount
{
    private readonly System.Threading.Lock _gate = new();
    private decimal _balance;
    private readonly List<string> _history = new();

    public BankAccount(decimal openingBalance)
    {
        if (openingBalance < 0)
            throw new ArgumentOutOfRangeException(nameof(openingBalance));
        _balance = openingBalance;
    }

    public void Withdraw(decimal amount)
    {
        if (amount <= 0)
            throw new ArgumentOutOfRangeException(nameof(amount));

        lock (_gate)
        {
            if (_balance < amount)
                throw new InvalidOperationException("Insufficient funds");

            _balance -= amount;
            _history.Add($"Withdrew {amount}");
        }
    }
}

While one caller holds _gate, another caller using that same lock must wait before checking the balance. The check, subtraction, and history update therefore cannot interleave with another protected withdrawal.

For older language/runtime versions, declare private readonly object _gate = new(); instead. All code reading or writing these shared fields must follow the same synchronization policy. A lock provides mutual exclusion, not rollback if a statement fails, and it doesn’t coordinate separate processes or database transactions.

Keep locks private and short. Avoid lock (this), lock (typeof(X)), or locking on strings because other code may lock the same object. Don’t do slow I/O inside the lock.

So far, the protected work has been synchronous. What if refreshing a shared cache needs to await a database call? You cannot await inside a lock. A SemaphoreSlim(1, 1) lets one caller enter an asynchronous critical section while the others wait asynchronously:

private readonly SemaphoreSlim _mutex = new(1, 1);

public async Task RefreshCacheAsync(CancellationToken ct)
{
    await _mutex.WaitAsync(ct);
    try
    {
        _cache = await LoadFromDatabaseAsync(ct);
    }
    finally
    {
        _mutex.Release();
    }
}

The first caller acquires the single slot, loads the data, and releases the slot in finally. Another refresh can then proceed. Readers of _cache still need an appropriate publication/read strategy. Protecting only writers does not make arbitrary mutable cache contents thread-safe.

Deadlocks

Locks protect our withdrawal, but they also introduce something we need to watch: waiting on each other. If one operation acquires two locks in one order and another acquires them in the opposite order, each can end up holding the lock the other needs:

flowchart LR
    A[Thread A] -->|Holds| L1[Lock 1]
    A -.->|Waits for| L2[Lock 2]
    B[Thread B] -->|Holds| L2
    B -.->|Waits for| L1
// Thread A: lock accounts, then lock audit.
// Thread B: lock audit, then lock accounts. Opposite order risks deadlock.

Acquire multiple locks in a consistent order everywhere, or redesign the operation to need fewer locks. Asynchronous waits can deadlock too if operations form a circular dependency; avoiding blocked threads alone does not remove that risk.

When the shared state is a collection

We now know how to protect a counter and a multi-step operation. In practice, the shared state is often a dictionary of counters, a cache, or a queue. We could put a lock around every access, but .NET already provides collections designed for concurrent use.

Ordinary List<T> and Dictionary<TKey, TValue> do not support unsynchronized concurrent mutation. Choose a collection that matches how you need to read and write the data:

Need Collection
Keyed lookup/cache ConcurrentDictionary<TKey, TValue>
FIFO operations ConcurrentQueue<T>
LIFO operations ConcurrentStack<T>
Unordered collection of results ConcurrentBag<T>

For example, suppose several workers process documents and update a shared count for each word. ConcurrentDictionary lets us express each per-word update with AddOrUpdate:

using System.Collections.Concurrent;

var wordCounts = new ConcurrentDictionary<string, int>();
Parallel.ForEach(documents, doc =>
{
    foreach (var word in doc.Split(' ', StringSplitOptions.RemoveEmptyEntries))
        wordCounts.AddOrUpdate(word, 1, (_, current) => current + 1);
});

Each call adds a missing word or updates its count without requiring a separate lookup followed by a write. That distinction matters: thread-safe methods don’t make a sequence such as “check then add” atomic. Prefer the collection’s atomic operations, and remember that stored mutable objects need their own synchronization.

GetOrAdd and AddOrUpdate factories may execute multiple times during contention. Avoid side effects. For an expensive async cache load, a stored Lazy<Task<T>> can defer starting the operation until the chosen value is used:

private readonly ConcurrentDictionary<string, Lazy<Task<Config>>> _configs = new();

public Task<Config> GetConfigAsync(string tenant) =>
    _configs.GetOrAdd(tenant,
        t => new Lazy<Task<Config>>(() => LoadConfigAsync(t))).Value;

This also caches a faulted task. Define eviction, retry, size limits, and cancellation ownership. A single request’s cancellation should not accidentally cancel a shared load that other callers still need.

Avoiding shared mutation altogether

Interlocked, locks, and concurrent collections help when workers must update shared state. Before adding more coordination, though, ask whether they need to share that state in the first place.

Suppose we’re calculating sales totals for several regions. Instead of having every operation update one shared grand total, let each calculate and return its own result. Once all operations finish, combine those results in one place:

var results = await Task.WhenAll(
    regions.Select(r => CalculateRegionTotalsAsync(r, ct)));
var grandTotal = results.Sum(r => r.Total);

Now the region calculations don’t compete to update grandTotal; the final sum happens after they complete. This assumes a suitably small or separately bounded set of regions and independent implementations. Immutable values and message passing reduce the need to coordinate mutation. A record isn’t automatically deeply immutable; its fields may still reference mutable lists or objects.

Use that same reasoning when choosing among these tools: first try to keep work independent. If sharing is necessary, identify what must happen atomically—a single value, a collection operation, or a whole business rule—and protect that boundary.

Stop work when its result is no longer needed

At this point, our operations can overlap, stay within limits, and coordinate their state. There’s one more part of their lifetime to handle: what happens when the caller no longer needs the result?

A client disconnect, shutdown, or timeout should be able to signal that work is no longer needed. Cancellation is cooperative: the operation must observe the token, and cancellation does not undo effects already committed.

flowchart LR
    U[User cancellation or timeout] --> CTS[CancellationTokenSource]
    CTS -->|Token| H[HTTP request]
    CTS -->|Token| D[Database query]
    CTS -->|Token| P[Parallel loop]
    H & D & P --> O[Observe cancellation and stop cooperatively]
using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
cts.CancelAfter(TimeSpan.FromSeconds(30));

try
{
    await Parallel.ForEachAsync(orders, new ParallelOptions
    {
        MaxDegreeOfParallelism = 8,
        CancellationToken = cts.Token
    }, async (order, ct) =>
    {
        await _invoiceService.SendAsync(order, ct);
    });
}
catch (OperationCanceledException) when (cts.IsCancellationRequested)
{
    _logger.LogInformation("Invoice run canceled or timed out");
    throw;
}

For synchronous computation, check periodically:

foreach (var chunk in chunks)
{
    ct.ThrowIfCancellationRequested();
    Process(chunk);
}

Task.Run(work, ct) can prevent queued work from starting if canceled early. It doesn’t interrupt a delegate already executing; that delegate must check the token too.

In an ASP.NET Core endpoint, the injected token represents HttpContext.RequestAborted:

app.MapGet("/report", async (ReportService reports, CancellationToken ct) =>
    await reports.BuildAsync(ct));

A detected client disconnect can cancel it, but detection isn’t instantaneous. Long-lived background jobs need their own cancellation and lifetime policy rather than keeping request-scoped resources alive.

Go further with asynchronous results

The dashboard needed all three responses before returning, so Task.WhenAll matched its purpose. Other I/O workflows need a different completion policy: process each response as it arrives, take the first completion, or inspect every failure. These choices change how we handle results; they don’t turn waiting into CPU parallelism.

Handling results as they finish: Task.WhenAny and Task.WhenEach

Since .NET 9, Task.WhenEach yields supplied tasks as they complete:

var tasks = urls.Select(url => _http.GetStringAsync(url, ct)).ToList();

await foreach (var finished in Task.WhenEach(tasks))
{
    try
    {
        var html = await finished;
        Console.WriteLine($"Got {html.Length} chars");
    }
    catch (OperationCanceledException) when (ct.IsCancellationRequested)
    {
        // Continue observing the remaining tasks as they complete.
    }
    catch (Exception ex)
    {
        _logger.LogError(ex, "Download failed");
    }
}

ct.ThrowIfCancellationRequested();

A yielded task can be faulted or canceled, so still await it to observe that outcome. WhenEach does not throttle the operations; for a huge URL collection, use bounded concurrency instead.

Task.WhenAny returns the first completed task, whether successful, faulted, or canceled. It does not mean “first successful response,” and it does not cancel the others:

using var race = CancellationTokenSource.CreateLinkedTokenSource(ct);

var primary = _http.GetStringAsync("https://primary.example.com/data", race.Token);
var backup = _http.GetStringAsync("https://backup.example.com/data", race.Token);
var requests = new[] { primary, backup };

try
{
    var winner = await Task.WhenAny(requests);
    return await winner; // Propagates this task's failure or cancellation too.
}
finally
{
    race.Cancel();
    try
    {
        await Task.WhenAll(requests); // Observe and drain both operations.
    }
    catch (Exception)
    {
        // Cleanup policy: outcomes are observed here; the winner's outcome
        // was already selected above. Add logging if needed.
    }
}

The cleanup waits for both operations to settle; cancellation is cooperative, not a forced termination. For a caller-side timeout on one task, WaitAsync(timeout, ct) is often simpler, but it also does not cancel the underlying work by itself.

Exceptions with Task.WhenAll

If tasks fault, the combined task retains their exceptions. Awaiting it propagates one exception; do not rely on which failure is selected. Cancellation needs a separate path because a canceled combined task has no AggregateException:

var all = Task.WhenAll(tasks);
try
{
    await all;
}
catch (OperationCanceledException) when (all.IsCanceled)
{
    _logger.LogInformation("Operation canceled");
    throw;
}
catch
{
    if (all.Exception is { } failures)
    {
        foreach (var ex in failures.Flatten().InnerExceptions)
            _logger.LogError(ex, "A task failed");
    }
    throw;
}

WhenAll waits for every supplied task even if one faults. It does not automatically cancel its siblings. Pass a shared token and define whether a failure should trigger cancellation.

Go further with CPU parallelism

The image loop established the basic idea: independent calculations can execute on available cores. The following techniques address more specific compute workloads. Use them when the shape of the calculation calls for them, rather than adding them to every parallel loop.

Thread-local state: summing without contention

Locking for every item serializes the aggregation:

long total = 0;
object gate = new();
Parallel.For(0, 1_000_000, i =>
{
    var value = Compute(i);
    lock (gate) { total += value; }
});

Local accumulation reduces synchronization:

long total = 0;

Parallel.For(0, 1_000_000,
    localInit: () => 0L,
    body: (i, state, subtotal) => subtotal + Compute(i),
    localFinally: subtotal => Interlocked.Add(ref total, subtotal));

Each local accumulator handles many iterations before it is combined with the total. The number of local accumulators is runtime-managed, not a fixed promise of one per physical core.

flowchart TD
    A[Worker-local subtotal A] -->|Interlocked.Add| T[(Total)]
    B[Worker-local subtotal B] -->|Interlocked.Add| T
    C[Worker-local subtotal C] -->|Interlocked.Add| T

Compute must be safe to run concurrently, and the total must fit in a long or use an appropriate overflow policy.

PLINQ: parallel LINQ

The loop works well when we want to process items. Sometimes our computation already reads naturally as a LINQ query—filtering numbers or producing invoices from in-memory data. PLINQ offers another way to express that CPU work:

var primes = Enumerable.Range(2, 5_000_000)
    .AsParallel()
    .Where(IsPrime)
    .ToList();

You can preserve order, cap concurrency, and support cancellation:

var report = orders
    .AsParallel()
    .AsOrdered()
    .WithDegreeOfParallelism(4)
    .WithCancellation(cancellationToken)
    .Select(o => BuildInvoice(o))
    .ToList();

Ordering has a cost. PLINQ can decide a sequential strategy is preferable for some query shapes. Keep the delegates independent and avoid querying a shared DbContext from them.

System.Numerics: parallelism inside a single core

The parallel loops and PLINQ examples spread work across workers and cores. Numeric workloads also have a different opportunity: one core can operate on several values with a vector instruction. This is data parallelism within a core, so it doesn’t depend on creating more worker threads.

SIMD means Single Instruction, Multiple Data. A vector instruction operates on multiple values at once. System.Numerics.Vector<T> exposes portable vector operations, with width depending on runtime and hardware:

flowchart LR
    subgraph Scalar[Scalar work]
        S1[Add value 0] --> S2[Add value 1] --> S3[Add value 2]
    end
    subgraph SIMD[Vector work]
        V1[Add several values in one vector operation] --> V2[Process next vector]
    end
using System.Numerics;

static float Sum(ReadOnlySpan<float> values)
{
    var accumulator = Vector<float>.Zero;
    int i = 0;

    for (; i <= values.Length - Vector<float>.Count; i += Vector<float>.Count)
        accumulator += new Vector<float>(values.Slice(i));

    float sum = Vector.Sum(accumulator);

    for (; i < values.Length; i++)
        sum += values[i];

    return sum;
}

Vector<float>.Count is not always eight. Also, vectorized summation changes the addition order, so floating-point rounding can differ from a scalar loop. Use an appropriate precision and reproducibility policy.

Many runtime and library operations already use vectorization where supported. System.Numerics.Tensors also provides primitives such as TensorPrimitives.Sum and Multiply. Measure before writing your own vector loop. You can combine SIMD inside chunks with parallelism across chunks.

The mistakes that bite people in production

1. Blocking on async code

// Blocks the caller while the operation is incomplete.
var user = _users.GetAsync(id).Result;

In UI apps and classic ASP.NET, blocking on an async method that needs the blocked synchronization context can deadlock. In ASP.NET Core, blocking wastes workers and can cause thread pool starvation under load.

flowchart TD
    R[Traffic increases] --> B[Requests block workers with Result or Wait]
    B --> E[Fewer workers available for new work]
    E --> G[Pool adapts but resource costs grow]
    G --> Q[Work queues and latency increase]
    Q --> F[Application appears unresponsive]

Use async throughout the call chain. For investigation, dotnet-counters, dotnet-stack, and dotnet-trace help distinguish thread pool starvation from CPU saturation or slow dependencies.

2. async void outside event handlers

// Caller cannot await completion or catch asynchronous failures normally.
public async void SaveAsync() { /* ... */ }

// Caller can await and observe failures.
public async Task SaveAsync() { /* ... */ }

async void is primarily for event-handler contracts requiring void. Its unhandled exceptions are delivered differently and can terminate an application. Even an async event handler should handle failures appropriately.

3. Fire-and-forget tasks

// No owner awaits completion or handles the task's failure.
_ = SendWelcomeEmailAsync(user);

The work can outlive scoped dependencies and be lost during shutdown. Use a supervised BackgroundService pipeline for best-effort in-process work, or a durable queue/job store when delivery matters. Discarding a task does not provide reliability.

4. Task.Run around I/O in ASP.NET Core

// Adds thread pool scheduling around an already asynchronous operation.
var data = await Task.Run(() => _http.GetStringAsync(url, ct));

// Use the asynchronous API directly.
var data = await _http.GetStringAsync(url, ct);

The first version doesn’t move a blocked network wait to another thread: the inner operation is already async. It adds unnecessary scheduling. Wrapping a genuinely blocking I/O call would instead occupy a pool worker for that wait.

Task.Run is useful for keeping a UI responsive during CPU work. On a server, it does not create extra CPU capacity; heavy computation may need bounded background processing or a separate service.

5. Parallel.ForEach with async lambdas

// The Action delegate becomes async void; completion is not tracked correctly.
Parallel.ForEach(urls, async url => await DownloadAsync(url));

Use Parallel.ForEachAsync with a deliberate limit, or Task.WhenAll for a small, bounded set of independent operations.

6. Unsafe collections in parallel loops

// Unsafe concurrent List<T>.Add calls.
var results = new List<Result>();
Parallel.ForEach(items, item => results.Add(Process(item)));

// Alternative: let PLINQ collect the results.
var safeResults = items.AsParallel().Select(Process).ToList();

The second version assumes Process is safe to execute concurrently. PLINQ doesn’t make the body thread-safe.

7. Sharing a DbContext across concurrent tasks

One context supports one operation at a time. Use separate contexts for independent concurrent units of work, and avoid passing tracked entities or scoped context instances into background workers.

For a related persistence concern, the soft deletes guide explains why query filtering and save interception need explicit boundaries too.

Choosing the right tool

flowchart TD
    Start[What kind of work?] --> Q{Waiting or computing?}
    Q -->|Waiting| I{Operation pattern?}
    Q -->|Computing| C{Work shape?}
    I -->|One| A[Await the operation]
    I -->|A few independent calls| B[Task.WhenAll]
    I -->|Many calls with a limit| D[Parallel.ForEachAsync]
    I -->|Continuous stream| E[Bounded channel and consumers]
    C -->|Independent loop| F[Parallel.For or ForEach]
    C -->|In-memory query| G[PLINQ]
    C -->|Numeric arrays| H[SIMD or TensorPrimitives]
    C -->|Keep UI responsive| J[Task.Run]
    A & B & D & E & F & G & H & J --> S{Shared mutable state?}
    S -->|No| Done[Measure and verify]
    S -->|Single atomic value| K[Interlocked]
    S -->|Collection operations| L[Concurrent collections]
    S -->|Synchronous critical section| M[lock]
    S -->|Asynchronous critical section| N[SemaphoreSlim]
Scenario Starting point
Three independent service calls Task.WhenAll
Results processed on completion Task.WhenEach (.NET 9+)
Thousands of calls, ten in flight Parallel.ForEachAsync
Limit a resource within one process SemaphoreSlim
Batch CPU-heavy image transforms Bounded Parallel.ForEach
Expensive in-memory filter/aggregate PLINQ
Large numeric arrays SIMD / TensorPrimitives
Keep desktop UI responsive await Task.Run(...)
Best-effort in-process background queue Bounded Channel<T> + BackgroundService
Thread-safe counter Interlocked
Concurrent keyed lookup ConcurrentDictionary
Synchronous multi-step invariant lock
Critical section containing await SemaphoreSlim(1, 1)

Summary

Concurrency keeps multiple operations in progress. Parallelism executes work simultaneously. Async I/O makes it possible to overlap waits without assigning a blocked worker to every operation; CPU parallelism uses compute capacity to finish independent work sooner.

  • Start with waiting vs computing before choosing an API.
  • Use Task.WhenAll for a small number of independent async operations, and Task.WhenEach when completion order matters.
  • Bound larger workloads with Parallel.ForEachAsync, semaphores, or channels. Concurrency limits and rate limits solve different problems.
  • Measure CPU parallelism, including its effect on the rest of the server.
  • Prefer independent state. Otherwise use atomic operations, concurrent collections, or critical sections that protect the whole invariant.
  • Propagate cancellation, observe task failures, and define who owns background work through shutdown.
  • Avoid blocking async code, unsupervised fire-and-forget work, and async lambdas passed to synchronous parallel loops.

Get those boundaries right and the toolbox becomes much easier to navigate.

Further reading

Found this useful? Share it

XLinkedIn

Discussion