Threads
Overview
A thread is a unit of execution that lives inside a process. While a process has its own isolated address space, multiple threads within the same process share certain resources — making threads lighter and faster to create and switch between than full processes. OS Course Notes — Weeks 3 & 5.md
What Threads Share vs. What They Own
| Resource | Shared across all threads | Per-thread (private) |
|---|---|---|
| Code (text segment) | ✓ | |
| Data segment | ✓ | |
| Heap | ✓ | |
| Stack | ✓ | |
| Registers (incl. program counter) | ✓ |
OS Course Notes — Weeks 3 & 5.md
Relationship to a Process
Each thread runs independently but operates inside the same process memory. OS Course Notes — Weeks 3 & 5.md
Benefits of Multithreading
- Responsiveness — one thread can keep the UI running while another does heavy work.
- Resource sharing — threads share memory cheaply without needing IPC mechanisms.
- Economy — creating and context-switching a thread is cheaper than doing so for a whole process.
- Multicore utilization — threads can run truly in parallel on multiple CPU cores.
OS Course Notes — Weeks 3 & 5.md
Downside: Synchronization Problems
Because threads share the heap and data segment, concurrent access to shared variables can produce race conditions — bugs where the outcome depends on the exact interleaving of thread execution. Proper synchronization (locks, semaphores, monitors) is required to prevent them. OS Course Notes — Weeks 3 & 5.md
⚠️ Race conditions are a key risk of multithreading. See the Key Concepts Glossary for a one-line definition.
How Threads Fit into the Bigger Picture
- Threads live inside a process, which is tracked by a PCB (Process Control Block).
- The CPU Scheduling algorithms (FCFS, SJF, Round Robin, Priority) generally operate at the thread/process level — the scheduler decides which thread gets the CPU next.
- Context switching between threads of the same process is cheaper than switching between different processes because the memory space does not change. OS Course Notes — Weeks 3 & 5.md
Exam Relevance
Threads are covered in the Week 3 material and are in scope for the midterm (Wed 22 April 2026). Review how threads differ from processes, what they share, and why synchronization is needed. See Exam Prep — Midterm for a full checklist. OS Course Notes — Weeks 3 & 5.md