# Concurrency

> Structuring a program so several tasks can be in progress at the same time.

- Level: Advanced
- Topics: Software
- URL: https://tidbit.legios.com.ar/concurrency/
- In Spanish: [Concurrencia](https://tidbit.legios.com.ar/es/concurrency/)

**Concurrency** means a program is dealing with several tasks whose lifetimes overlap: while one waits for a file, a database or the network, another can make progress. A web server handling thousands of visitors, or an app that keeps scrolling smoothly while it downloads images, depends on it. The usual tools are threads, separate processes, and `async`/`await`, which lets a single thread pause a task at each slow step and switch to another.

It is related to, but not the same as, **parallelism**. Concurrency is about structure: juggling many tasks, even on a single CPU core by switching between them. Parallelism is about execution: literally running things at the same instant on several cores. A concurrent program can run in parallel if the hardware allows it, but it does not have to. Go's co-creator Rob Pike summed it up as "concurrency is about dealing with lots of things at once; parallelism is about doing lots of things at once".

The hard part is shared data. If two tasks read a bank balance of 100, each adds 50, and each writes back its result, the balance ends at 150 instead of 200: a **race condition**, where the result depends on who happened to go first. Programmers prevent it with **locks** (only one task may touch the data at a time), atomic operations, or designs that avoid sharing at all, like passing messages. Overdo the locks and you risk a **deadlock**, where two tasks each wait forever for the other's lock. These bugs are notorious because they may appear once in a million runs.

## An analogy

A cook preparing three dishes. While the pasta boils she chops for the salad, then checks the oven: one person, several dishes in progress. Two cooks would be parallelism, and two cooks reaching for the same pan is a race condition.

## Keep in mind

- Several tasks in progress at once, often while others wait on slow I/O.
- Concurrency is structure; parallelism is running at the same instant.
- Shared data invites race conditions; locks prevent them but can deadlock.
- Concurrency bugs are often rare and hard to reproduce.

## Go deeper

- [Concurrent computing on Wikipedia](https://en.wikipedia.org/wiki/Concurrency_(computer_science))
- [Concurrency is not parallelism (Rob Pike, Go blog)](https://go.dev/blog/waza-talk)
- [Race condition on Wikipedia](https://en.wikipedia.org/wiki/Race_condition)

## Related terms

- [Database](https://tidbit.legios.com.ar/database/): An organized store of data that programs can search and update safely.
- [Server](https://tidbit.legios.com.ar/server/): A computer whose job is to answer requests from other computers, all day, every day.
- [Cores and threads](https://tidbit.legios.com.ar/cpu-cores-and-threads/): Why a CPU is really several processors, and why it can juggle even more tasks.
- [Go](https://tidbit.legios.com.ar/go/): A simple, fast programming language from Google, built for servers and cloud tools.
- [Bug](https://tidbit.legios.com.ar/bug/): A mistake in software that makes it behave differently from what was intended.
