Course map

Topic 006 / Core

Concurrency and parallelism

Chapter 1.3 · Concurrency and Thread Safety

What and why

Concurrency is structuring a program to deal with many in-flight tasks at once — they may take turns, not literally overlap. Parallelism is tasks literally executing at the same instant, which requires more than one core. A single-core machine can be highly concurrent but never parallel; a multi-core machine can be both. This topic compares the common models for writing concurrent programs so a later design choice (how a server handles many connections, → 10) has a shared vocabulary. Builds on: → 5 (processes and threads).

Diagram

Model How it handles many tasks Good for
Thread-per-task one OS thread per task low to moderate concurrency, simple code
Thread pool fixed threads, tasks queued bounding resource use under load
Event loop one thread, non-blocking I/O, callbacks very high concurrency, I/O-bound work
Async/await syntax over an event loop or coroutines same as event loop, easier to read

How it works

  1. Concurrency is about structure — can the program make progress on task B while task A is waiting on something (disk, network)?
  2. Parallelism is about hardware — are task A and task B's instructions actually executing on different cores at the same instant?
  3. Thread-per-task is the simplest mental model but doesn't scale past some thousands of tasks — each thread costs memory and scheduling overhead.
  4. A thread pool bounds the number of threads and queues excess work, trading some latency under burst load for a hard cap on resource use.
  5. An event loop handles many waiting tasks on very few threads by never blocking — it registers interest in "call me back when this socket is ready" instead of a thread sitting idle while it waits.
  6. Async/await is the same event-loop model with syntax that reads top-to-bottom instead of as nested callbacks — the underlying concurrency is unchanged.

Practical example

(illustrative). A proxy server holding 10,000 concurrent client connections, most of them idle waiting on a slow upstream: a thread-per-connection design needs roughly 10,000 threads, each with its own stack — real memory spent on tasks doing nothing but waiting. An event-loop design holds the same 10,000 connections on a handful of threads, because a waiting connection costs only a small data structure, not a whole thread.

When to use it

  • Use thread-per-task or a small thread pool when: concurrency is modest and the code's simplicity matters more than raw connection count.
  • Use an event loop or async/await when: the workload is I/O-bound and concurrency is high (many waiting connections, few actually computing).
  • Use true parallelism (multiple cores, multiple processes) when: the workload is CPU-bound — more threads on one core doesn't speed up CPU-bound work.
  • Avoid: blocking calls inside an event loop — one blocking database call stalls every other task sharing that thread.
  • Failure scenarios: a single slow synchronous call accidentally placed inside an async handler, serializing every request behind it; a thread pool sized without a bound, defeating the reason to use one.
  • Simpler alternative: before choosing a concurrency model, check whether the workload is even large enough to need one — a single synchronous thread handling a handful of requests a minute needs none of this.

Practice

(decision — judge scenarios and defend the choice; 15–20 min, no lab services). For each of four scenarios — a CPU-heavy image-resizing service, a proxy holding tens of thousands of mostly-idle connections, a low-traffic internal admin tool, and a service that's a mix of both — pick thread-per-task, a thread pool, or an event loop/async, and state in one sentence what bottleneck that choice avoids. Done when each choice names a concrete bottleneck, and no two answers give the same justification for different scenarios.

Key takeaway

Concurrency is a program's structure for handling many tasks; parallelism is hardware actually running them at once — pick thread-per-task, a pool, or an event loop based on whether the work is I/O-bound or CPU-bound and how much concurrency it needs. Done when you can: name the right model for a described workload and say what breaks if you pick wrong. Used next in → 7 (race conditions), → 10 (client–server connection handling).

References

None — this topic makes no external claims.

Source directory

Search the guide