Concurrency is structuring a program as several activities that make progress in overlapping time; parallelism is running them at the same instant on several cores. Both are needed to use modern hardware and to handle many things at once, and both bring bugs that appear only sometimes and are hard to reproduce.
Key concepts
- Threads and processes: units the operating system schedules, with shared or separate memory.
- Shared state and locks: mutexes protect data, but they invite deadlock and contention.
- Message passing: activities share nothing and talk through channels or mailboxes.
- Async and event loops: one thread juggles many waiting tasks with callbacks, futures or coroutines.
- Memory model and atomics: what one thread may see of another's writes, and the operations that make it safe.
- Data parallelism: split the same work across cores, then combine the results.
Vocabulary
- Race condition: a result that depends on the timing of unsynchronised accesses.
- Deadlock: tasks each waiting for something another holds, so none can move.
- Atomic: an operation that appears to happen all at once.
- Amdahl's law: the serial part of a task limits the speed-up from adding cores.
What to study, in order
- Start two threads that increment one counter, watch it go wrong, then fix it with a lock.
- Build a producer and consumer with a queue, then again with channels.
- Cause a deadlock on purpose, then fix it with a consistent lock order.
- Write the same server with threads and with an event loop; compare behaviour under load.
- Read your language's memory model and learn what an atomic guarantees.
- Use a race detector or thread sanitiser on your own code.