Threads
What is a Thread?
A thread is the smallest unit of CPU execution within a process.
In simple words:
Thread = A single sequence of instructions executing inside a process.
A process can contain one or multiple threads.
Process +-------------------+ | Code | | Data | | Heap | | Open Resources | | | | Thread 1 | | Thread 2 | | Thread 3 | +-------------------+
Threads belonging to the same process generally share the process's:
- Code
- Data
- Heap
- Open files
- Other process resources
But each thread has its own execution state, including:
- Program counter
- CPU registers
- Stack
Process vs Thread
This is one of the most important OS concepts.
Process
A process is an execution environment that owns resources.
Thread
A thread is an execution unit within that process.
Think of it like:
PROCESS │ ├── Code ├── Data ├── Heap ├── Files │ ├── Thread 1 ├── Thread 2 └── Thread 3
The process provides the resources; threads perform the work.
Real-Life Example
Consider a web browser.
A browser may have multiple processes and multiple threads.
Within a process, different threads might handle tasks such as:
Browser Process │ ├── UI Thread ├── Network Thread ├── Rendering Thread └── Background Thread
This allows different activities to progress concurrently.
Why Do We Need Threads?
Suppose a program performs several tasks:
Task A → Read network data Task B → Update interface Task C → Process data Task D → Write to disk
With a single thread:
Task A ↓ Task B ↓ Task C ↓ Task D
Some tasks may have to wait.
With multiple threads:
Process / | \ ↓ ↓ ↓ Thread 1 Thread 2 Thread 3 ↓ ↓ ↓ Network UI Processing
The OS can schedule these threads so that the program remains responsive and work can overlap or execute in parallel when multiple CPU cores are available.
Thread Components
A thread has its own execution context.
+----------------------+ | Thread | +----------------------+ | Program Counter | | CPU Registers | | Stack | | Thread State | +----------------------+
1. Program Counter
Stores the address of the next instruction to execute.
2. CPU Registers
Contain the thread's current CPU state.
3. Stack
Each thread normally has its own stack.
The stack stores things such as:
- Local variables
- Function-call information
- Return addresses
4. Thread State
A thread can have states such as:
New Ready Running Blocked/Waiting Terminated
The exact implementation depends on the OS.
What Do Threads Share?
Threads within the same process commonly share:
+-------------------------+ | Process | +-------------------------+ | Code ← Shared | | Data ← Shared | | Heap ← Shared | | Open Files ← Shared | | Resources ← Shared | +-------------------------+ | Stack ← Own | | Registers ← Own | | Program Counter← Own | +-------------------------+
This sharing is one reason threads can communicate efficiently.
But it also creates synchronization problems because multiple threads can access the same shared data.
Single-Threaded Process
A process with only one thread is called a single-threaded process.
Process | └── Thread 1
Example:
Program ↓ One thread ↓ Execute instructions
Multithreaded Process
A process containing multiple threads is a multithreaded process.
Process │ ├── Thread 1 ├── Thread 2 ├── Thread 3 └── Thread 4
Each thread can perform a different part of the application's work.
Types of Threads
There are two classical categories:
- User-Level Threads
- Kernel-Level Threads
1. User-Level Threads
User-level threads are managed primarily by a user-space thread library rather than directly by the kernel scheduler.
Conceptually:
Process | +-------------------+ | User Thread Lib | +-------------------+ ↓ ↓ ↓ T1 T2 T3
The kernel may see the process or underlying kernel execution entities differently depending on the threading implementation.
Advantages
- Fast thread creation
- Fast thread switching
- Thread management can be performed in user space
- Kernel does not need to perform every thread-management operation
Disadvantages
A major limitation of some pure user-level threading models is:
If one thread makes a blocking system call, the entire process may become blocked.
Also, pure user-level threads may not be able to take advantage of multiple CPU cores unless the implementation maps them to multiple kernel execution entities.
2. Kernel-Level Threads
Kernel-level threads are managed and scheduled by the operating system kernel.
Conceptually:
Process | +-- Thread 1 ──┐ +-- Thread 2 ──┼── Kernel Scheduler +-- Thread 3 ──┘ ↓ CPU
The kernel knows about the individual threads.
Advantages
- One blocked thread does not necessarily block other threads of the process
- Threads can be scheduled independently
- Can take advantage of multiple CPU cores
- Better integration with kernel scheduling
Disadvantages
- Thread operations can involve kernel overhead
- More resources may be required
Modern general-purpose operating systems commonly use kernel-managed threads, though implementation details differ.
User-Level vs Kernel-Level Threads
| Feature | User-Level Threads | Kernel-Level Threads |
|---|---|---|
| Managed by | User-space library/runtime | OS kernel |
| Kernel awareness | May be limited | Kernel knows threads |
| Creation/switching | Usually faster | More overhead |
| Blocking behavior | Can be problematic in pure models | Other threads can continue |
| Multi-core execution | Depends on mapping | Naturally supported |
| Scheduling | User-space runtime | Kernel scheduler |
Thread Mapping Models
Operating systems and threading systems can map user threads to kernel execution entities in different ways.
Three classical models are:
- Many-to-One
- One-to-One
- Many-to-Many
1. Many-to-One
Many user threads are mapped to one kernel thread.
User Threads T1 ──┐ T2 ──┼──→ Kernel Thread T3 ──┤ T4 ──┘
Advantage
- Simple
- Low overhead
Disadvantage
If the kernel thread blocks, all user threads may be blocked.
Also, the threads cannot execute truly in parallel on multiple cores through that single kernel thread.
2. One-to-One
Each user thread maps to one kernel thread.
T1 ─────→ K1 T2 ─────→ K2 T3 ─────→ K3 T4 ─────→ K4
Advantages
- Better parallelism
- One blocked thread doesn't necessarily block the others
Disadvantages
- Higher overhead
- Too many threads can consume significant system resources
Many modern operating systems use a model broadly corresponding to one user thread to one kernel thread.
3. Many-to-Many
Many user threads are mapped onto a pool of kernel threads.
User Threads Kernel Threads T1 ──┐ T2 ──┼──────────────→ K1 T3 ──┤ T4 ──┼──────────────→ K2 T5 ──┤ T6 ──┘
This can provide flexibility in how user-level concurrency maps to kernel-level execution.
Thread Scheduling
The OS scheduler may select threads for execution, depending on the OS's process/thread model.
Example:
Ready Threads ↓ +----+----+----+ | T1 | T2 | T3 | +----+----+----+ ↓ Scheduler ↓ CPU
On a multi-core CPU:
Core 1 → Thread 1 Core 2 → Thread 2 Core 3 → Thread 3 Core 4 → Thread 4
Therefore, multiple threads can execute in parallel when there are multiple available cores.
Concurrency vs Parallelism
These concepts are important when discussing threads.
Concurrency
Multiple tasks make progress during overlapping periods.
Time → T1: ███ ███ T2: ███ ███
They don't necessarily execute at exactly the same instant.
Parallelism
Multiple tasks execute at the same time on different CPU cores.
Core 1 → Thread 1 ███████ Core 2 → Thread 2 ███████
Remember
Concurrency = dealing with multiple tasks
Parallelism = executing multiple tasks simultaneously
Benefits of Multithreading
1. Responsiveness
A program can continue responding while another thread performs a long-running operation.
Example:
UI Thread ↓ User interaction Worker Thread ↓ Large calculation
The interface can remain responsive.
2. Resource Sharing
Threads share the process's resources.
This can make communication between threads easier than communication between completely separate processes.
3. Economy
Creating and switching between threads can be less expensive than creating and switching between processes, depending on the OS and implementation.
4. Scalability
Threads can take advantage of multiple CPU cores.
Process | +-- T1 → Core 1 +-- T2 → Core 2 +-- T3 → Core 3 +-- T4 → Core 4
Problems with Threads
Threads are powerful, but they introduce challenges.
1. Race Conditions
Multiple threads access shared data at the same time and produce unexpected results.
Example:
Shared counter = 10 Thread A → reads 10 Thread B → reads 10 Thread A → writes 11 Thread B → writes 11
Expected result might be:
12
but the actual result can become:
11
This is a race condition.
2. Synchronization
Threads may need synchronization mechanisms such as:
- Mutex
- Semaphore
- Monitor
- Condition variable
- Read-write lock
These help coordinate access to shared resources.
3. Deadlocks
Threads can become stuck waiting for resources held by each other.
Thread A → waiting for Resource B Thread B → waiting for Resource A
Neither can continue.
Deadlocks are covered later in your roadmap.
4. Debugging Complexity
Multithreaded programs can be difficult to debug because execution order can vary.
Process vs Thread — Important Exam Table
| Feature | Process | Thread |
|---|---|---|
| Definition | Program in execution | Execution unit within a process |
| Memory | Own address space | Shares process address space |
| Resources | Owns/manages resources | Uses process resources |
| Stack | Process has execution stacks through its threads | Each thread has its own stack |
| Registers | Execution context | Own registers/context |
| Communication | More expensive | Usually easier within same process |
| Creation | Generally more expensive | Generally cheaper |
| Isolation | Stronger | Weaker |
| Failure | Often isolated from other processes | A serious thread failure can affect its process |
Example: Web Browser
A modern browser may use multiple processes, and each process may contain multiple threads.
A simplified model:
Browser │ ├── Process A │ ├── UI Thread │ └── Network Thread │ ├── Process B │ ├── Rendering Thread │ └── Worker Thread │ └── Process C ├── Background Thread └── Other Thread
This architecture helps provide:
- Responsiveness
- Parallelism
- Resource isolation
- Better fault containment
The exact architecture depends on the browser.
Thread Life Cycle
Like processes, threads can move through execution states.
+------+ | New | +------+ | ↓ +-------+ | Ready | +-------+ | ↓ +---------+ | Running | +---------+ / \ ↓ ↓ Waiting Terminated | ↓ Ready
Thread Control Block
The OS may maintain a Thread Control Block (TCB) or equivalent thread-specific data structure.
A TCB can contain information such as:
+----------------------+ | Thread Control Block | +----------------------+ | Thread ID | | Thread State | | Program Counter | | CPU Registers | | Stack Information | | Scheduling Info | +----------------------+
The exact structure varies by operating system.
Thread vs Process — Simple Example
Imagine a restaurant.
Restaurant = Process Workers = Threads
The restaurant has shared resources:
Kitchen Tables Equipment Ingredients
Each worker performs a different task:
Worker 1 → Cooking Worker 2 → Taking orders Worker 3 → Serving
They share the restaurant's resources but perform different activities.
Similarly:
Process ├── Thread 1 → Task A ├── Thread 2 → Task B └── Thread 3 → Task C
Important Terms
| Term | Meaning |
|---|---|
| Thread | Smallest unit of CPU execution within a process |
| Multithreading | Using multiple threads in a process |
| User-level thread | Managed primarily by user-space software |
| Kernel-level thread | Managed by the OS kernel |
| TCB | Thread Control Block |
| Concurrency | Multiple tasks making progress |
| Parallelism | Multiple tasks executing simultaneously |
| Race condition | Incorrect result caused by unsynchronized shared access |
| Synchronization | Coordinating concurrent execution |
Exam Definition
A thread is the smallest unit of CPU execution within a process. Threads belonging to the same process share resources such as code, data, heap, and open files, while each thread maintains its own program counter, registers, and stack.
One-line memory trick
Process = Resource container
Thread = Execution unit
Process │ ├── Shared Code ├── Shared Data ├── Shared Heap │ ├── Thread 1 → Own Stack + Registers ├── Thread 2 → Own Stack + Registers └── Thread 3 → Own Stack + Registers