OS/Lesson 3 of 9/4 min read
Threads and Concurrency
Threads vs processes, user and kernel threads, multithreading models, concurrency vs parallelism, and thread pools.
On this page
What is a Thread
A thread is the smallest unit of CPU execution inside a process. A process can have many threads that share its code, data, heap and open files, but each thread has its own:
- Thread ID
- Program counter
- Registers
- Stack
That is why threads are called lightweight processes.
Thread vs Process
| Aspect | Process | Thread |
|---|---|---|
| Memory | Separate address space | Shares process address space |
| Creation cost | High (new page tables, PCB) | Low |
| Context switch | Expensive (TLB flush, page table switch) | Cheap |
| Communication | Needs IPC | Shared memory, direct |
| Isolation | A crash usually affects only itself | A crash kills the whole process |
| Example | Each Chrome tab (process per site) | Worker threads in a web server |
Multithreading
Multithreading means running several threads inside one process.
Benefits:
- Responsiveness: the UI thread stays live while a background thread loads data.
- Resource sharing: threads share memory by default.
- Economy: cheaper to create and switch than processes.
- Scalability: threads can run in parallel on multiple cores.
Costs: shared data needs synchronization, otherwise you get race conditions and hard-to-reproduce bugs.
User-Level Thread
Managed by a user-space library; the kernel sees only one process.
- Very fast to create and switch (no system call).
- If one thread makes a blocking system call, the whole process blocks.
- Cannot use multiple cores, because the kernel schedules the process as one unit.
Kernel-Level Thread
Managed and scheduled by the kernel (Linux threads via clone, Windows threads).
- True parallelism on multicore; one blocking thread does not block the others.
- Slower to create and switch (needs kernel involvement).
Multithreading models
How user threads map to kernel threads:
- Many-to-one: many user threads on one kernel thread. Fast, but no parallelism and one block blocks all.
- One-to-one: each user thread has a kernel thread. Used by Linux and Windows. Parallel, but too many threads costs kernel resources.
- Many-to-many: M user threads multiplexed on N kernel threads. Flexible; Go goroutines and Java virtual threads follow this idea.
Concurrency vs Parallelism
- Concurrency: dealing with many tasks at once by interleaving them. Possible on a single core via time slicing.
- Parallelism: doing many tasks at the same instant on multiple cores.
Concurrency (1 core): A A B B A B A B
Parallelism (2 cores): core1: A A A A
core2: B B B BConcurrency is about structure, parallelism is about execution. Parallel speedup is limited by the serial part of the work; see Amdahl's Law.
Thread Pool
A thread pool pre-creates a fixed number of worker threads that pull tasks from a queue.
- Avoids the cost of creating a thread per request.
- Bounds the number of threads so the system is not overwhelmed.
- Used by web servers, Java
ExecutorService, database connection handlers.
Common thread problems
- Race condition: result depends on timing of threads. See synchronization.
- Deadlock: threads wait on each other forever. See deadlocks.
- Starvation: a thread never gets the resource it needs.
- False sharing: two threads write different variables on the same cache line, causing cache ping-pong.
Quick revision
- Threads share code, data, heap, files; each has its own stack, registers, PC.
- Kernel threads give parallelism; user threads are cheaper but block together.
- Linux uses the one-to-one model.
- Concurrency = interleaving; parallelism = simultaneous.