DevPath
Tracks

Processes, Threads, and Concurrency

Processes versus threads, how race conditions lose updates, how locks fix them, how deadlock happens, and how an event loop gets concurrency from a single thread.

intermediate45 min#concurrency#threads#locks#deadlock#async#event-loop

Prerequisites: How Computers Run Programs, Memory, References, and Pointers

Why it matters

Servers handle thousands of users at once, apps stay responsive while downloading, and data jobs use every core. All of that is concurrency, and it is also where the nastiest bugs live: ones that appear once in a thousand runs, vanish under a debugger, and corrupt data silently. A clear mental model of processes, threads, locks, and event loops is the difference between code that works on your laptop and code that works in production.

Intuition

A process is a restaurant: its own kitchen, ingredients, and rules. Two restaurants cannot accidentally use each other's flour. A thread is a cook inside one restaurant. All cooks share the same kitchen, which makes cooperation fast (pass a plate) and dangerous (two cooks grab the last egg at once).

A race condition is two cooks both reading the recipe card "eggs left: 1", both cracking one, and both writing "eggs left: 0". One egg was used twice. A lock is a single key to the pantry: only the holder can enter, others wait.

An event loop is one cook with a to-do list who never stands idle. When a task says "wait for the oven", the cook starts the next task and returns when the oven beeps.

How it works

Processes vs threads

  • A process has its own memory address space and the OS isolates it. A crash usually stays contained. Communication needs explicit channels such as pipes, sockets, or shared memory. Starting one is relatively heavy.
  • Threads in a process share heap memory and open files but each has its own stack. They are cheap to create and share data easily, so every shared write needs care.
  • The OS scheduler switches between runnable threads whenever it likes. You cannot assume an order.
One processisolated from every other process
Thread 1 (own stack)
Thread 2 (own stack)
Shared heap, globals, open filesevery thread reads and writes here
Inside a process, threads keep separate stacks but share the heap, which is why they cooperate cheaply and also why shared writes need care. A different process has its own copy of all this and cannot touch it directly.

CPU-bound vs I/O-bound. CPU-bound work (image processing) needs more cores, so use processes, or threads in languages without a global lock. I/O-bound work (HTTP calls, disk) spends most of its time waiting, so threads or async let other work proceed. In CPython, the global interpreter lock lets only one thread run Python bytecode at a time, so threads help I/O-bound code but not CPU-bound code; use multiprocessing for the latter.

Race conditions. The classic one is the lost update. counter += 1 is really three steps: read, add, write.

 Thread A: read 0 ........... write 1
 Thread B:      read 0 ... write 1        final = 1, expected 2
Thread AcounterThread Bread0read0write 0 + 1 = 1write 0 + 1 = 1
Both threads read 0 before either writes. Each writes 1, so one increment is lost: the counter ends at 1 instead of 2.

Locks (mutexes). Wrap the read-modify-write in a critical section guarded by a lock so only one thread runs it at a time. Keep critical sections short, since waiting threads do nothing.

Thread AlockcounterThread Bacquiregrantedacquire (must wait)read 0, write 1releasegrantedread 1, write 2release
With the lock, B cannot start until A has finished its read-modify-write. B reads 1, not 0, so the final value is 2.

Deadlock needs four conditions together: mutual exclusion, hold-and-wait, no preemption, and circular wait. The textbook case: thread A holds lock 1 and wants lock 2 while thread B holds lock 2 and wants lock 1. Both wait forever. The usual cure is to break circular wait by always acquiring locks in one global order.

held bywantsheld bywantsAL2BL1
A is thread A, B is thread B. A holds lock 1 and wants lock 2, while B holds lock 2 and wants lock 1. Following the arrows goes in a circle, and that cycle is the circular wait.

Async and the event loop. Single-threaded runtimes like JavaScript and Python's asyncio run tasks one at a time and switch only at await. That removes most data races, but not all: if you read state, await, then write based on the old read, other tasks can change it in between. The demo below shows exactly that.

OperationTimeNotes
Create a processHeavy (ms)Own address space; isolated
Create a threadLight (tens of microseconds)Shares memory; needs synchronization
Uncontended lock acquireVery cheap (tens of ns)Cost grows sharply under contention
Context switchMicrosecondsPlus cold caches afterward

Code: lost updates and a lock

Real thread races are timing-dependent, which makes them poor demos. Async tasks interleave only at await, so the same bug becomes perfectly repeatable. Each of five tasks reads the counter, yields, then writes back its read plus one.

import asyncio
 
state = {"n": 0}
 
 
async def unsafe_inc():
    v = state["n"]          # read
    await asyncio.sleep(0)  # yield: other tasks run here
    state["n"] = v + 1      # write a stale value
 
 
async def safe_inc(lock):
    async with lock:        # only one task in this block at a time
        v = state["n"]
        await asyncio.sleep(0)
        state["n"] = v + 1
 
 
async def main():
    await asyncio.gather(*(unsafe_inc() for _ in range(5)))
    print("unsafe", state["n"])
 
    state["n"] = 0
    lock = asyncio.Lock()
    await asyncio.gather(*(safe_inc(lock) for _ in range(5)))
    print("safe", state["n"])
 
 
asyncio.run(main())

Both versions print two lines: unsafe 1 and safe 5. All five unsafe tasks read 0 before any of them wrote, so four increments were lost. With the lock, each read-modify-write completes before the next starts.

With real threads the idea is identical: in Python you would wrap the increment in with threading.Lock():, and the result becomes the exact expected total on every run.

Common pitfalls

  • Check-then-act: if key not in cache: cache[key] = load() is a race. Two threads can both see the key missing. Lock around the whole check and act.
  • Locking too much: one giant lock makes threads run one by one. Lock the smallest region that protects the invariant.
  • Inconsistent lock order: acquiring A then B in one function and B then A in another is a deadlock waiting for load.
  • Forgetting to release: use with in Python or try/finally so an exception cannot leave the lock held.
  • Blocking the event loop: a long synchronous computation or time.sleep inside an async function freezes every other task.
  • Assuming threads speed up everything: CPU-bound Python threads fight over the global lock and can be slower than one thread.

Practice

Spot the race easy

Hint

Break the statement into reads and writes of shared data.

Show solution

balance = balance - amount runs in two threads for withdrawals of 10 from 100. Both may read 100 and both write 90, so one withdrawal vanishes. Fix by locking the whole read-check-write, or by using an atomic compare-and-set.

Fix a deadlock medium

Hint

Which of the four deadlock conditions is easiest to remove?

Show solution

Transfer money between two accounts, each with its own lock. If transfer(a, b) locks a then b and a concurrent transfer(b, a) locks b then a, they deadlock. Always lock in a fixed order, for example by account id: lock min(id_a, id_b) first. Circular wait becomes impossible.

Bounded producer-consumer medium

Hint

You need to block producers when full and consumers when empty.

Show solution

Use a queue with a maximum size. Producers put, which blocks when full; consumers get, which blocks when empty. Thread-safe queues (queue.Queue in Python) handle the locking and condition signaling. Add a sentinel value so consumers know when to stop.

Limit concurrent requests hard

Hint

A semaphore is a lock that admits N holders instead of one.

Show solution

Fetching 1000 URLs with all requests at once overwhelms the server. Create a semaphore with 10 permits (asyncio.Semaphore(10)). Each task acquires a permit before its request and releases it after. At most 10 requests are in flight, and the rest wait their turn.

Interview questions

  1. Process versus thread? Processes have separate memory and strong isolation; threads share memory, are cheaper, and need synchronization.
  2. What is a race condition and how do you prevent it? Outcome depends on the timing of unsynchronized access to shared state. Prevent with locks, atomics, immutability, confinement to one thread, or message passing.
  3. What are the conditions for deadlock? Mutual exclusion, hold and wait, no preemption, circular wait. Break any one; lock ordering breaks circular wait.
  4. Why is JavaScript single-threaded but still handles many connections? Slow work (network, disk) is handed to the runtime or OS, and callbacks or promises run on the one thread when results arrive.
  5. When use multiprocessing instead of threads? CPU-bound work in runtimes with a global lock, or when you want fault isolation.

Interviewers look for: naming the read-modify-write breakdown, asking whether work is CPU-bound or I/O-bound, proposing lock ordering for deadlocks, and knowing that async removes thread races but not logical races across await.

Quick check

In an async program, a task reads a shared counter, awaits a network call, then writes the counter plus one. Several such tasks run together. What can go wrong?

Finished this lesson?

Further resources

Related lessons