⭐ Multithreading (MT)
1. Definition
Multithreading is a processor technique that allows a single CPU or core to execute multiple threads concurrently, improving CPU utilization and throughput.
A thread is the smallest unit of execution within a program, with its own program counter, registers, and stack, but sharing the process’s memory.
2. Purpose
- Hide long-latency operations: e.g., memory access or I/O waits.
- Improve CPU utilization: When one thread stalls, another can execute.
- Increase throughput: Multiple threads progress in parallel.
3. Types of Multithreading
A) Fine-Grained Multithreading
- Switches between threads every CPU cycle.
- Advantage: Hides latency effectively.
- Disadvantage: Single thread executes slower, as CPU is shared every cycle.
B) Coarse-Grained Multithreading
- Switches threads only on long-latency events (e.g., cache misses).
- Advantage: Less context-switching overhead.
- Disadvantage: CPU may be idle if no thread stalls.
C) Simultaneous Multithreading (SMT)
- Multiple threads issue instructions in the same cycle using the same pipeline.
- Example: Intel Hyper-Threading Technology.
- Advantage: Maximizes resource utilization in superscalar pipelines.
4. How Multithreading Works
- Each thread has its own program counter and registers.
- Threads share execution units, caches, and memory.
- CPU switches between threads rapidly (fine-grained) or on stalls (coarse-grained).
- In SMT, instructions from multiple threads can be issued simultaneously to different functional units.
5. Benefits of Multithreading
- Better CPU utilization: Keeps pipelines busy when one thread stalls.
- Higher throughput: More instructions executed per unit time.
- Reduced branch and memory stall penalties: Another thread can execute during a stall.
- Cost-effective: Multiple threads share a single core, unlike multi-core designs.
6. Challenges / Limitations
- Resource contention: Threads compete for functional units, caches, or memory bandwidth.
- Complex scheduling: Requires hardware or OS support to switch threads efficiently.
- Security risks: Shared caches and resources can lead to side-channel attacks (e.g., Spectre, Meltdown).
- Not all workloads benefit: Single-thread heavy workloads may not see improvement.
7. Relation to Other Concepts
| Concept |
Relation to Multithreading |
| Thread-Level Parallelism (TLP) |
Multithreading is a key technique to achieve TLP. |
| Cache Coherency |
Important when threads share data in multicore systems. |
| Speculative Execution |
Can work together with multithreading to improve pipeline utilization. |
| Superscalar Pipelines |
SMT allows multiple threads to issue instructions simultaneously in a superscalar core. |
8. Example
Assume two threads, T1 and T2:
| Cycle |
Pipeline Stage |
| 1 |
T1 fetch |
| 2 |
T2 fetch, T1 decode |
| 3 |
T1 execute, T2 decode |
| 4 |
T2 execute |
- Fine-grained: Switch threads every cycle → hides stalls.
- Coarse-grained: Switch thread only when one stalls → less frequent context switching.
9. Exam-Friendly Summary
- Multithreading: Concurrent execution of multiple threads on a single CPU/core.
- Types: Fine-grained, Coarse-grained, Simultaneous (SMT).
- Goal: Improve CPU utilization, hide stalls, increase throughput.
- Limitations: Resource contention, complexity, limited benefit for single-threaded workloads.