Part 5 of 6 · 3 min read
Benefits
Why programs use threads
The textbook lists four benefits:
| Benefit | Meaning | Example |
|---|---|---|
| Responsiveness | One thread keeps working while another waits | A text editor saves a file in a thread while the user types |
| Resource sharing | Threads share memory without extra calls | A cache in memory that all request handlers use |
| Economy | Threads are cheaper to create and switch to | A server that starts a thread for each request |
| Scalability | Threads of one process can run on several processors at once | A video encoder that splits a frame between four processors |
In plain words: Two words are often mixed up and worth separating. Concurrency means that several tasks are in progress at the same time, which can be done by a single processor switching between them quickly. Parallelism means that several tasks are actually running at the same instant, which needs several processors. A multithreaded program on one processor is concurrent but not parallel. On four processors it can be both.
The balance of the two decides the benefit. Threads that spend most of their time waiting for the network or the disk gain from concurrency even on one processor, because one thread can run while others wait (the idea of Level 0, page 1). Threads that compute all the time gain only if there are several processors, and the gain has a ceiling (page 6).
Concurrency and parallelism
Concurrency is the property of having several tasks in progress during the same period, whether or not they run at the same instant. Parallelism is the execution of several tasks at the same instant on several processors.
Example: two threads on one processor take turns, so they are concurrent but not parallel; on two processors they can also run in parallel.
Interview angle
Answer in your head first, then open it.
What is the difference between a process and a thread?
A process has its own address space. Threads in a process share the address space but each has its own stack and registers.
Why are threads cheaper than processes?
No copy of the address space is needed, and switching between threads of one process keeps the same address space.
What is the main risk of threads?
Shared data can be corrupted when threads change it at the same time, and one thread's crash ends the process.
What is the difference between concurrency and parallelism?
Concurrency is overlapping progress; parallelism is simultaneous execution.
Check yourself: 1. Name the four benefits of threads. 2. Can a program be concurrent but not parallel? 3. Which kind of thread gains from several processors: one that waits for the network, or one that computes?
Words you will meet
| Word | Meaning |
|---|---|
| Thread | A unit of execution inside a process |
| Multithreaded | A process with more than one thread |
| TID | The number of a thread |
| Concurrency | Several tasks in progress in the same period |
| Parallelism | Several tasks running at the same instant |
| Race condition | A wrong result caused by the timing of threads (Level 4) |
Quick notes
- A thread is a unit of execution inside a process.
- A thread ID (TID) is the number the kernel assigns to each thread, which names that thread in the kernel's tables.
- Concurrency is the property of having several tasks in progress during the same period, whether or not they run at the same instant.