CS Fundamentals Trainer · Guides

Concurrency: races, deadlocks and the event loop

Threads, event loops and goroutines compared; the lost update and the deadlock as animations you can step through; what the fixes cost, measured in Go and Python; and the one rule of an event loop.

27 min read

Concurrency is about several things being in progress at once: a server handling a thousand requests, a program waiting on a database while it also reads a file, a UI that stays responsive while it downloads. It is also the source of the bugs people find hardest to reproduce, because the program is correct on most runs and wrong on a few. This guide builds the vocabulary (threads, event loops, goroutines), shows the two classic failures (the race condition and the deadlock) with animations you can step through, measures the fixes, and ends with the questions interviewers ask.

The numbers below come from small programs run once for this guide, on a 2-core Linux container with Go 1.24, Python 3.13 and Node 22. They are illustrations of a behaviour, not benchmarks: times and counts change from run to run and from machine to machine, and a ratio measured here does not carry over to your workload. The code for every experiment is in the appendix so you can run it yourself.

1. Concurrency, parallelism, and three models

Concurrency is structuring a program as several tasks that make progress in overlapping time. Parallelism is running several tasks at the same instant on different CPU cores. You can have concurrency without parallelism (one core switching between tasks) and parallelism is only useful if the work can be split.

Diagram: Three common ways to run many tasks at once

ThreadsEvent loopGoroutines (Go)Actors (Erlang, Akka)
Runs onOS threads, scheduled by the OSone threadmany lightweight tasks multiplexed over a few OS threads by the Go runtimemany lightweight processes, scheduled by the runtime
Shared memoryyes, by defaultyes, but one callback runs at a timeyes, unless you avoid it: channels are the idiomatic way to hand data over, but nothing stops two goroutines touching one variableno: each actor owns its state and others can only send it messages
Uses several coresyes (not in CPython, see section 2)no, unless you add workers or processesyesyes
Classic bugdata races, deadlockblocking the loop, logical races across awaitdata races on shared variables, deadlock on channels and locks, leaked goroutinesa full mailbox, message ordering, deadlock through request cycles
ExamplesJava, C# Thread, Python threadingNode.js, Python asyncio, browsersGoErlang/Elixir, Akka

The actor model is a way of organising state and communication: state is private to an actor and everything else happens through messages. Go's goroutines are a lighter kind of task that can be used in that style (Go's proverb is "do not communicate by sharing memory; share memory by communicating"), but a Go program may share memory too, and then it must synchronise it like any threaded program.

C# async/await is not the same thing as a single-threaded event loop. After an await, the rest of the method may continue on a thread-pool thread, in parallel with other code, unless the caller runs in a context that brings it back to one thread (a UI thread, classic ASP.NET). So C# code that shares data between tasks can have real data races and needs the tools of section 3. Note that lock cannot contain an await; use SemaphoreSlim (await gate.WaitAsync()) to guard an asynchronous critical section.

A task is I/O-bound when it mostly waits (network, disk, database) and CPU-bound when it mostly computes. Waiting costs nothing if the model can run something else meanwhile, which is why one Node.js process can serve thousands of connections. Computing needs cores, which is why a CPU-bound Node.js handler needs a worker thread or a separate process (section 5).

2. Race conditions: the lost update

Two terms are often used as if they were one:

Fixing one does not fix the other. A lock around each variable removes the data race but not necessarily the race condition; protecting the invariant (section 3) addresses both.

The classic form of a race is the lost update. counter += 1 looks like one step, but the machine does three: read the value, add one, write it back. If two threads interleave those steps, both read the same old value and one of the increments disappears.

Step through it below. Without the lock, both threads read 0, both compute 1, and the counter ends at 1. Tick the box to protect the update with a lock and run it again.

Interactive simulation: lost update

The same bug in real code. Four tasks each add one a million times; the answer should be 4,000,000.

counter := 0
var wg sync.WaitGroup
for w := 0; w < 4; w++ {
	wg.Add(1)
	go func() {
		defer wg.Done()
		for i := 0; i < 1_000_000; i++ {
			counter++ // read, add, write: not one step
		}
	}()
}
wg.Wait()
fmt.Println(counter)
int counter = 0;
var tasks = Enumerable.Range(0, 4).Select(_ => Task.Run(() =>
{
    for (int i = 0; i < 1_000_000; i++)
        counter++; // read, add, write: not one step
}));
await Task.WhenAll(tasks);
Console.WriteLine(counter);

Run three times in Go, the counter ended at 2,800,244, 2,252,535 and 2,278,252 instead of 4,000,000. That is 30 to 44 percent of the increments lost, and different every time. Go can point at the bug for you: build or run with -race (go run -race .) and the race detector prints WARNING: DATA RACE with the two conflicting accesses and the goroutines that made them. Use it in tests and CI.

The bug hides in Python (and then it does not)

In CPython the global interpreter lock (GIL) lets only one thread run Python bytecode at a time. Four threads each running counter += 1 a million times lost nothing in three runs (4,000,000 every time). That does not make the code correct: the interpreter may switch threads between bytecodes, and a free-threaded build without the GIL is available as an experimental option since Python 3.13 (PEP 703). Any pause between the read and the write exposes the problem at once:

def read_modify_write():
    global counter
    for _ in range(N):
        value = counter       # read
        time.sleep(0)         # any call that lets another thread run
        counter = value + 1   # write back

With 4 threads and N = 200,000 (800,000 expected), the unprotected version ended at 200,004, 200,000 and 200,000, losing about three quarters of the updates: every thread read the same value in each round and all but one write was overwritten. With with lock: around the three lines the answer was exactly 800,000. The lesson: a test that passes does not prove a race is absent.

Check-then-act, with no threads at all

Race conditions are not only a threads problem. Single-threaded JavaScript has one too, because every await is a point where other code runs. There is no data race here (only one callback touches balance at any instant), but the invariant "the balance never goes below zero" is broken:

let balance = 100;
async function withdraw(amount) {
  if (balance >= amount) {   // check
    await audit(amount);     // other callbacks run here
    balance -= amount;       // act on information that is now stale
  }
}
await Promise.all([withdraw(80), withdraw(80)]);
console.log(balance);        // -60

Both calls passed the check while the balance was still 100. This is called check-then-act (or time-of-check to time-of-use), and the same shape appears in databases ("select, then update") and file systems ("does it exist? then create it"). Section 3 shows the fixes.

3. Fixing races: four tools

  1. Mutual exclusion (a lock, mutex). Only one task may be inside the critical section at a time. Simple and general, but it makes tasks wait and it is the cause of deadlocks (section 4).
  2. Atomic operations. The hardware performs read-modify-write as one indivisible step. Fast, but limited to simple values such as counters and flags.
  3. Message passing and ownership. Do not share the data. One task owns it and the others send it requests (a channel, a queue, an actor mailbox). Go's saying is "do not communicate by sharing memory; share memory by communicating".
  4. Immutability. Data that never changes cannot race. Share it freely and build a new copy to change it.
lock = threading.Lock()

def bump():
    global counter
    for _ in range(N):
        with lock:            # critical section
            counter += 1
var mu sync.Mutex
mu.Lock()
counter++
mu.Unlock()

// or, for a counter, one atomic instruction:
var n atomic.Int64
n.Add(1)
private static readonly object Gate = new();
lock (Gate)
{
    counter++;
}

// or, for a counter, one atomic instruction:
Interlocked.Increment(ref counter);

For JavaScript's check-then-act, make the whole check-and-act step run one caller at a time. A promise chain works as a tiny lock: with it, the two withdrawals above end at 20 (the second is refused), not -60.

let tail = Promise.resolve();
function exclusive(fn) {
  const run = tail.then(fn);
  tail = run.catch(() => {});   // a failure must not block the next caller
  return run;
}
await Promise.all([80, 80].map((a) => exclusive(() => withdraw(a))));

What the fixes cost (one illustrative run)

The same four-goroutine, 4,000,000-increment Go program, made correct three ways. This is a single run of a micro-benchmark on a 2-core container, so read it as a picture of the shape of the trade-off. The full program is in the appendix.

ApproachResultTime (one run)
No protection2.3 to 2.8 million (wrong)3 ms
sync.Mutex4,000,000230 ms
atomic.Int644,000,00060 ms
One goroutine owns the counter, others send it messages over a buffered channel4,000,000296 ms

The wrong answer is fastest because nobody waits. In this run the atomic took about a quarter of the mutex's time, and the channel version was the slowest per increment. Do not turn that into a rule such as "atomics are four times faster than mutexes". The gap depends on how many tasks fight over the same variable (contention), on the CPU and its cache behaviour, on the language runtime and its lock implementation, and above all on how much work happens inside the protected section: when the section does real work, the cost of the lock itself is a small part and the atomic's advantage disappears. An atomic also cannot protect an invariant that spans several fields, which a mutex or a single owner can. A channel costs more per operation, but it moves ownership so that one goroutine can own a whole data structure without any lock.

In Python the lock made the four-thread loop slower too, by a factor that varied between roughly 2 and 4 across repeated runs, but that comparison belongs to CPython with its GIL and says little about other runtimes.

Measure your own workload, with the real amount of contention and the real work in the critical section, before choosing a tool for speed.

Rules of thumb:

4. Deadlock

A deadlock is two or more tasks each waiting for something the other holds, so none can ever continue. The textbook case needs two locks. Thread 1 takes A and then wants B. Thread 2 takes B and then wants A. If each gets its first lock before the other gets its second, both wait forever.

Step through it, then tick the box to make both threads take the locks in the same order.

Interactive simulation: deadlock

A deadlock needs all four of these conditions (Coffman, 1971) at once. Remove any one and it cannot happen:

ConditionMeaningWhat stops it (prevention)
Mutual exclusiona resource is used by one task at a timeuse immutable, read-only or lock-free data, or give each task its own copy
Hold and waita task holds one resource while waiting for anotheracquire everything at once, or hold at most one lock at a time
No preemptiona resource cannot be taken away from its holderrare for ordinary locks; some systems can force it (a database aborts a transaction and releases its locks)
Circular waitA waits for B, B waits for Agive all locks a global order and always take them in that order

There are three different strategies, and it helps not to mix them up:

Use prevention where you can, and recovery (timeouts, cancellation, rollback and retry) as a safety net, not as the design.

Go detects the simplest case at runtime. When every goroutine is blocked, the program stops with fatal error: all goroutines are asleep - deadlock! and a stack trace for each. That only fires when all of them are stuck. In a real server other goroutines keep running, so a deadlocked pair just silently stops serving its requests. Most languages give no message at all: a deadlocked service shows up as threads that never finish, a growing request queue and low CPU use. A thread dump (jstack, dotnet-dump, py-spy dump, sending SIGQUIT to a Go program) lists what each thread is waiting for and finds it.

A close relative is livelock: tasks are not blocked, but keep reacting to each other and make no progress (two people stepping aside in a corridor, both in the same direction, forever). Randomised backoff fixes it. Starvation is when a task never gets its turn because others always win the lock; fair locks and bounded queues address it.

5. The event loop and the one rule

An event loop runs one callback at a time on one thread. When a callback needs to wait (a network reply, a timer), it registers what to do next and returns, and the loop runs the next ready callback. That is why await is cheap, and why two callbacks of one event loop cannot touch the same memory at the same instant: nothing else runs until you return or await. This rules out data races between callbacks, but not race conditions: another callback can run at every await, so a check made before an await may be stale after it (the balance example in section 2). Worker threads with shared memory (SharedArrayBuffer) and runtimes whose async code spreads over several threads, such as C#, bring real data races back.

Diagram: The event loop: ready callbacks run one at a time

The price is one rule: never block the loop. A callback that computes for 500 ms, or calls a blocking function, holds up every other task, including ones that were ready long before.

In Node.js a timer asked to fire after 100 ms, while a synchronous loop kept the thread busy for 500 ms, ran after 504 ms. The same 500 ms of waiting done with await let the timer fire on time, after 100 ms.

setTimeout(() => console.log("timer"), 100);
while (performance.now() < until) {}               // blocks: the timer is late
await new Promise((r) => setTimeout(r, 500));      // yields: the timer is on time

Python's asyncio behaves the same. A ticker that should print every 0.1 s was delayed by a time.sleep(0.5) call inside another coroutine: its first tick came at 0.50 s instead of 0.10 s. Replacing it with await asyncio.sleep(0.5) gave ticks at 0.10, 0.20 and 0.30 s as expected.

async def blocking():
    time.sleep(0.5)            # freezes every task on the loop

async def friendly():
    await asyncio.sleep(0.5)   # the loop runs other tasks meanwhile

Things that quietly block the loop: a CPU-heavy loop or large JSON parse, a synchronous file or database driver, requests.get in an async def, a regular expression with catastrophic backtracking. The fixes are to use the async version of the library, or to move the work off the loop: a worker thread (worker_threads, asyncio.to_thread, Task.Run) for blocking calls, a separate process for heavy computation.

A related trap in C# is async-over-sync: calling .Result or .Wait() on a task from code that runs on a context which allows only one piece of code at a time (a classic ASP.NET or UI thread). The blocked thread waits for the task, and the task needs that same thread to finish. It is a deadlock with a single lock. The fix is to use await all the way up the call chain.

6. Choosing a model

WorkloadGood fitWhy
Many connections, mostly waiting (web server, chat, API gateway)event loop or goroutineswaiting is cheap; thousands of tasks, few threads
Heavy computation (image processing, parsing, encoding)processes or threads on several coresneeds cores; in CPython use multiprocessing or a free-threaded build, because of the GIL
Blocking libraries you cannot changethread poolkeeps the blocking off the event loop
Pipelines of stageschannels or queuesthe data moves, nothing is shared

Tasks are not equally expensive. In Go, 100,000 parked goroutines took about 2.0 KiB of stack each and were created in under 400 ms, so spawning one per request is normal. OS threads are heavier: each reserves its own stack (often 8 MiB of address space by default on Linux), and the scheduler has to switch between them. Starting 2,000 Python threads took 0.17 s here, and a server with tens of thousands of threads spends its time switching rather than working. That is why thread-per-request servers use a bounded pool and why event loops and goroutines exist.

Size a pool on purpose, but treat any formula as a starting estimate. For CPU-bound work a first guess is about one worker per core; more only adds switching. For work that spends time waiting, a rough estimate is cores * (1 + wait time / compute time). It assumes steady load, waits that do not depend on each other, and no limit downstream. Real systems break those assumptions: bursts make queues grow, work varies in size, a database or an API you call may allow only a fixed number of connections (which then caps the useful pool size whatever the formula says), and the runtime may behave differently (the GIL, an event loop). So start from the estimate, then load test and watch queue length and latency percentiles, and set the limit against the capacity of what the pool calls.

7. Other things that go wrong

8. Common mistakes

  1. Assuming counter += 1, x = x + 1 or if (not exists) create is atomic.
  2. Trusting a test that passed: a race may need thousands of runs, or a different CPU, to show.
  3. Locking the variable instead of the invariant, so two related updates still interleave.
  4. Holding a lock across slow I/O or while calling code you do not control.
  5. Taking several locks without a global order.
  6. Blocking the event loop with CPU work or a synchronous library.
  7. Mixing sync and async: .Result and .Wait() in C#, asyncio.run inside a running loop, time.sleep in a coroutine.
  8. Starting unlimited tasks, or tasks nobody waits for or cancels.
  9. "Fixing" a race with sleep: it only moves the timing.

9. Things to mention in an interview

Questions interviewers like to add:

Quick recap

  1. Concurrency is overlapping tasks; parallelism is running them on several cores.
  2. Threads share memory, event loops run one callback at a time, goroutines can share memory or pass messages over channels, and actors only pass messages.
  3. A data race is unsynchronised access to shared memory; a race condition is any timing-dependent bug, and it can exist without a data race. counter += 1 is read, add, write, and two of them can lose an update (30 to 44 percent lost in the Go experiment).
  4. A passing test proves little: the Python GIL hid the lost update in 12 million increments and a single sleep(0) exposed it at once.
  5. Fix races with a lock, an atomic, a single owner with messages, or immutable data, and keep critical sections small.
  6. A deadlock needs mutual exclusion, hold and wait, no preemption and circular wait; a global lock order prevents the circle, and timeouts or rollback recover when it happens anyway.
  7. Never block an event loop: a 500 ms busy loop made a 100 ms timer fire at 504 ms.
  8. Bound the concurrency, pass cancellation, and look at the results of background tasks.

Appendix: the experiment code

Every number above came from one of these programs. Run them yourself; expect different numbers on a different machine, and different numbers on each run.

Go: lost update, mutex, atomic and channel

Run with go run . (counts and times) and go run -race . (the race detector report). Save it as main.go in a folder with a go.mod (go mod init race).

package main

import (
	"fmt"
	"sync"
	"sync/atomic"
	"time"
)

const (
	workers = 4
	perWork = 1_000_000
)

func run(name string, work func(wg *sync.WaitGroup)) {
	var wg sync.WaitGroup
	start := time.Now()
	for i := 0; i < workers; i++ {
		wg.Add(1)
		go work(&wg)
	}
	wg.Wait()
	fmt.Printf("%-8s took %v\n", name, time.Since(start).Round(time.Millisecond))
}

func main() {
	want := workers * perWork

	for trial := 0; trial < 3; trial++ {
		plain := 0
		run("plain", func(wg *sync.WaitGroup) {
			defer wg.Done()
			for i := 0; i < perWork; i++ {
				plain++
			}
		})
		fmt.Printf("  expected %d got %d lost %d\n", want, plain, want-plain)
	}

	var mu sync.Mutex
	locked := 0
	run("mutex", func(wg *sync.WaitGroup) {
		defer wg.Done()
		for i := 0; i < perWork; i++ {
			mu.Lock()
			locked++
			mu.Unlock()
		}
	})
	fmt.Printf("  expected %d got %d\n", want, locked)

	var atom atomic.Int64
	run("atomic", func(wg *sync.WaitGroup) {
		defer wg.Done()
		for i := 0; i < perWork; i++ {
			atom.Add(1)
		}
	})
	fmt.Printf("  expected %d got %d\n", want, atom.Load())

	// one goroutine owns the counter; the others send it messages
	ch := make(chan struct{}, 1024)
	done := make(chan int)
	go func() {
		n := 0
		for range ch {
			n++
		}
		done <- n
	}()
	run("channel", func(wg *sync.WaitGroup) {
		defer wg.Done()
		for i := 0; i < perWork; i++ {
			ch <- struct{}{}
		}
	})
	close(ch)
	fmt.Printf("  expected %d got %d\n", want, <-done)
}

Python: the GIL and the lost update

Run python3 race.py. The first part is the plain counter += 1 loop; set explicit to True to use the read, pause, write version (it is slow: about a minute with the lock, so lower N if you are impatient).

import sys, threading, time

explicit = False
N = 200_000 if explicit else 1_000_000
counter = 0
lock = threading.Lock()

def bump(use_lock):
    global counter
    for _ in range(N):
        if use_lock:
            lock.acquire()
        if explicit:
            value = counter          # read
            time.sleep(0)            # lets another thread run
            counter = value + 1      # write back
        else:
            counter += 1
        if use_lock:
            lock.release()

def run(use_lock, threads=4):
    global counter
    counter = 0
    ts = [threading.Thread(target=bump, args=(use_lock,)) for _ in range(threads)]
    start = time.perf_counter()
    for t in ts: t.start()
    for t in ts: t.join()
    return counter, time.perf_counter() - start

print(sys.version.split()[0], "GIL enabled:", getattr(sys, "_is_gil_enabled", lambda: True)())
for use_lock in (False, True):
    for _ in range(3):
        got, secs = run(use_lock)
        print(f"lock={use_lock} expected {4 * N:,} got {got:,} in {secs:.2f}s")

JavaScript: a check-then-act race and a blocked event loop

Run each file with node (Node 22 or later; the files are ES modules, so name them .mjs).

// race.mjs
const audit = () => new Promise((resolve) => setTimeout(resolve, 10));
let balance = 100;
async function withdraw(amount) {
  if (balance >= amount) {
    await audit();
    balance -= amount;
  }
}
await Promise.all([withdraw(80), withdraw(80)]);
console.log("unprotected:", balance);        // -60

And the blocked event loop:

// blocked.mjs: a timer asked for 100 ms behind 500 ms of busy work
const start = performance.now();
setTimeout(() => console.log(`timer ran after ${Math.round(performance.now() - start)} ms`), 100);
const until = performance.now() + 500;
while (performance.now() < until) { /* busy */ }
console.log(`busy loop finished at ${Math.round(performance.now() - start)} ms`);
// replace the busy loop with:  await new Promise((r) => setTimeout(r, 500));
// and the timer runs after about 100 ms

Python asyncio: blocking the loop

import asyncio, time

async def ticker(start):
    for _ in range(3):
        await asyncio.sleep(0.1)
        print(f"  tick at {time.perf_counter() - start:.2f}s")

async def blocking(start):
    time.sleep(0.5)            # blocks the whole event loop

async def friendly(start):
    await asyncio.sleep(0.5)   # yields while waiting

async def main(work):
    start = time.perf_counter()
    await asyncio.gather(ticker(start), work(start))

asyncio.run(main(blocking))
asyncio.run(main(friendly))

Go: the cost of a parked goroutine

package main

import (
	"fmt"
	"runtime"
	"sync"
	"time"
)

func main() {
	const n = 100_000
	var before, after runtime.MemStats
	runtime.GC()
	runtime.ReadMemStats(&before)
	start := time.Now()
	stop := make(chan struct{})
	var wg sync.WaitGroup
	for i := 0; i < n; i++ {
		wg.Add(1)
		go func() { defer wg.Done(); <-stop }()
	}
	time.Sleep(200 * time.Millisecond)
	runtime.ReadMemStats(&after)
	fmt.Printf("%d goroutines parked: %.1f KiB each (stacks), created in under %v\n",
		n, float64(after.StackInuse-before.StackInuse)/1024/n, time.Since(start).Round(10*time.Millisecond))
	close(stop)
	wg.Wait()
}

Sources

  1. The Go Project, The Go Memory Model. When a write by one goroutine is guaranteed to be seen by another, and why racy programs are unsafe.
  2. Go documentation, Data Race Detector. The -race flag used in section 2.
  3. Python documentation, threading and asyncio.
  4. Sam Gross, PEP 703: Making the Global Interpreter Lock Optional in CPython.
  5. Node.js documentation, The Node.js Event Loop.
  6. E. G. Coffman, M. J. Elphick and A. Shoshani, "System Deadlocks", ACM Computing Surveys 3(2), 1971. The four conditions in section 4.
  7. Microsoft, Managed threading best practices. Locks, Interlocked and deadlock guidance for .NET.

Practise this topic: CS Fundamentals

More guides