Programming languages / Concurrency vs parallelism

Concurrency vs parallelism

Two tasks, one core, then two cores. A timeline shows the difference between juggling several tasks and truly running them at the same instant, and which kind of work each one speeds up.

Intermediate7 steps~6 min
Task A — a piece of work to get done
Task B — a second, separate piece of work
Core — runs one task at any instant
// THE SHORT VERSION

In 30 seconds

Concurrency is dealing with many tasks over the same period: a single core runs a bit of one, then a bit of another, so all of them make progress.

Parallelism is doing many things at the same instant, which takes more than one core.

Tasks that mostly wait for a network or a disk (I/O-bound) gain from concurrency alone, because the core can work on something else while they wait. Tasks that compute the whole time (CPU-bound) only finish sooner with more cores.

Concurrency vs parallelism, step by step

STEPS01 / 07

Time units are illustrative. Real cores switch tasks many times a second, each switch has a small cost, and extra cores help only as far as the work can be split into independent parts.

STEP 01 / 07 Two tasks, one at a time
THE MATH
A = 4 units, B = 4 units one core, A then B done at 4 + 4 = 8
  1. Step 1: Two tasks, one at a time. Start with two tasks that each need 4 units of computing, and one processor core to run them. The simplest plan is to finish A, then start B. It works, but B makes no progress at all until time 4.
  2. Step 2: Concurrency: take turns. Concurrency means dealing with several tasks over the same period. The core runs a slice of A, switches to B, and back again. Both tasks move forward, yet at any instant only one is running, so the total is still 8. Each switch even costs a little.
  3. Step 3: Parallelism: two cores. Parallelism means doing several things at the very same instant, and that needs more than one core. Give A and B a core each and they run side by side. The same 8 units of work are finished at time 4.
  4. Step 4: I/O-bound: mostly waiting. Many tasks spend most of their time waiting for input or output (I/O): a network reply, a disk, a user. They are called I/O-bound. While A waits, the core is free, so it can run B. On one core, taking turns now finishes at 5 instead of 8.
  5. Step 5: CPU-bound vs I/O-bound. Put the finishing times side by side. For CPU-bound work, which computes the whole time, taking turns on one core gains nothing: only a second core helps. For I/O-bound work, taking turns already recovers most of the lost time without any extra core.
  6. Step 6: Both together. Real programs mix the two. Here four tasks of 2 units each share two cores. Each core takes turns between two tasks, which is concurrency, and the two cores run at the same instant, which is parallelism. Concurrency is how the work is organised; parallelism is how it is executed.
  7. Step 7: Which one do you need?. Ask what the tasks spend their time on. A web server juggling thousands of slow connections is I/O-bound: it needs concurrency, and one core goes a long way. Resizing a thousand photos is CPU-bound: it needs parallelism, so it needs more cores.
VALUES SO FAR
task4 units of workinput
in orderdone at 81 core
turns····1 core
parallel····2 cores
I/O task····input
turns····I/O
mixed····both

Spotted a mistake on this page?