Juggling many tasks versus doing many at once, and the bugs it brings: race conditions and deadlock.
Concurrency is dealing with many tasks at once, by switching between them. Parallelism is running many tasks at the same instant on different CPU cores.
A computer with a single core can still "do many things at once". It runs one task for a very short time, then switches to another, and so on, so fast that all of them seem to move together. This is concurrency: a way to structure a program as several tasks in progress. With several cores, tasks can truly run at the same moment. That is parallelism: a way to make the work faster by doing parts simultaneously.
You can have concurrency without parallelism (one core, many tasks), parallelism without much concurrency (one big job split into equal pieces), or both. A server that handles thousands of requests with a few threads that wait for the network (I/O) is concurrent. A video encoder that uses all 8 cores is parallel.
| Concurrency | Parallelism | |
|---|---|---|
| Idea | Many tasks in progress, taking turns | Many tasks running at the same instant |
| Needs several cores? | No | Yes |
| Main goal | Handle many things, hide waiting | Finish one big job faster |
| Good for | I/O-heavy work (network, disk) | CPU-heavy work (encoding, maths) |
| Typical tools | Threads, async/await, event loops | Multiple processes, thread pools on many cores, GPUs |
| Main risk | Races and deadlocks on shared data | The same risks, plus limits from the part that cannot be split |
| Bug | What happens | Typical cures |
|---|---|---|
| Race condition | The result depends on the order of steps. A read-modify-write is cut in the middle, so an update is lost | A lock around the whole step, an atomic operation (atomic increment, compare-and-swap), or avoid sharing |
| Deadlock | Tasks wait in a circle for locks that others hold, and none can move | Take locks in one fixed order, hold fewer locks, use timeouts, or avoid nested locks |
| Starvation | A task never gets its turn because others keep winning | Fair locks, queues, priorities that age |
Simplified and deterministic. The order of steps is chosen by the buttons, not at random. Real schedulers can pick any order, which is why these bugs appear only sometimes. Each task is a made-up 4 units of work.
| Term | Meaning in easy words |
|---|---|
| Concurrency | Several tasks in progress at the same time, usually by taking turns |
| Parallelism | Several tasks running at the very same moment on different cores |
| Process | A running program with its own memory |
| Thread | A unit of work inside a process. Threads of one process share its memory |
| Race condition | A bug where the result depends on the timing of tasks |
| Critical section | Code that touches shared data and must not be interrupted by others |
| Lock / mutex | A key that only one task can hold, to protect a critical section |
| Atomic operation | One step that cannot be cut in the middle (such as an atomic increment) |
| Deadlock | Tasks wait for each other in a circle, and none can continue |
| Starvation | A task never gets its turn |
| Context switch | The CPU stops one task and starts another. It has a small cost |
| Choice | We gain | We lose |
|---|---|---|
| One big lock | Simple and safe | Little parallelism, everyone waits |
| Many small locks | More work in parallel | More chance of deadlock and mistakes |
| Atomic operations | Fast, no waiting for simple updates | Only fit simple cases |
| Message passing, no shared data | No races on shared memory | Copying data and a different style of design |
| More threads | More work in progress | More switching and memory, and diminishing returns |
| Optimistic updates with retry | No waiting when conflicts are rare | Wasted work when conflicts are common |
count++ is read, add, write: three steps.