OS Notes
Comprehensive guide to multithreading concepts including threading challenges, thread safety, synchronization issues, threading patterns, and practical multithreading in modern applications.
Introduction
Your web browser is a masterclass in multithreading. While you scroll through a page, one thread renders the visible content, another downloads images in the background, a third runs JavaScript animations, a fourth handles your mouse clicks, and a fifth checks for updates. If any of these tasks ran sequentially, browsing would feel painfully slow — you would wait for every image to download before you could scroll.
Multithreading is the ability of a process to execute multiple threads concurrently. On a multi-core processor, threads can run truly in parallel on different cores. On a single-core processor, the OS rapidly switches between threads (like process context switching but faster), creating the illusion of simultaneous execution.
Concurrency vs Parallelism
These terms are often confused but have distinct meanings:
Concurrency: Multiple tasks making progress (not necessarily simultaneously). On a single core, tasks are interleaved — only one runs at any instant, but they appear to run together.
Parallelism: Multiple tasks actually executing at the same physical instant on different cores.
| Core 1 | [T1][T2][T1][T3][T2][T1][T3][T2] |
| Core 1 | [T1][T1][T1][T1][T1][T1] |
| Core 2 | [T2][T2][T2][T2][T2][T2] |
| Core 3 | [T3][T3][T3][T3][T3][T3] |
| Core 4 | [T4][T4][T4][T4][T4][T4] |
A multi-threaded application achieves concurrency on any system but achieves parallelism only on multi-core hardware.
Threading Challenges
Multithreading introduces several challenges that do not exist in single-threaded programs:
1. Race Conditions
When two threads access shared data simultaneously and at least one is writing, the result depends on the order of execution — which is unpredictable.
Why? counter++ is actually three operations: read counter, add 1, write back. If threads interleave between these steps, increments are lost.
2. Deadlocks
Two threads each holding a lock the other needs (covered in detail in the Deadlocks chapter).
3. Starvation
A thread never gets access to a shared resource because other threads always get it first.
4. Priority Inversion
A low-priority thread holding a resource needed by a high-priority thread effectively blocks the high-priority thread.
Thread Safety
Code is thread-safe if it functions correctly when called from multiple threads simultaneously. Strategies for thread safety:
Using Mutex Locks
Using Atomic Operations
Using Thread-Local Storage
Each thread gets its own copy of the variable — no sharing, no conflict.
__thread int thread_local_counter = 0; // Each thread has its own copyMultithreading Patterns
1. Thread Pool Pattern
Create a fixed number of worker threads at startup. Tasks are submitted to a queue, and idle workers pick them up. Avoids the overhead of creating/destroying threads for each task.
2. Producer-Consumer Pattern
Producer threads generate data and put it in a shared buffer. Consumer threads take data from the buffer and process it. A bounded buffer with semaphores coordinates them.
3. Fork-Join Pattern
A parent thread forks into multiple child threads that work in parallel, then joins (waits for all children to complete) before continuing.
Amdahl's Law
Not all programs benefit equally from multithreading. If a program is P% parallelizable, the maximum speedup with N cores is:
The serial portion limits the maximum possible speedup, regardless of how many cores you add.
Real-World Multithreading Examples
| Application | Thread Roles |
|---|---|
| Web Server (Apache) | One thread per client connection |
| Web Browser | UI thread, rendering thread, network thread, JS thread |
| Game Engine | Physics thread, rendering thread, audio thread, AI thread |
| Database (MySQL) | One thread per query + background threads for logging |
| IDE (VS Code) | UI thread, language server thread, file watcher thread |
Real-World Analogy
Multithreading is like a restaurant kitchen with multiple cooks sharing the same workspace. They all share the pantry (heap), recipe book (code), and kitchen equipment (resources). Each cook works on their own dish (has their own task/stack), but they must coordinate — they cannot both use the only oven at the same time (mutual exclusion), and they should not grab ingredients another cook already measured out (race condition).
Key Takeaways
- Multithreading enables concurrent execution within a single process
- Concurrency (interleaving) differs from parallelism (true simultaneous execution)
- Race conditions occur when threads access shared data without proper synchronization
- Thread safety is achieved through locks, atomic operations, or thread-local storage
- Common patterns include thread pools, producer-consumer, and fork-join
- Amdahl's Law limits the speedup from parallelism based on the serial fraction of code
- Multithreading adds complexity — synchronization bugs are among the hardest to debug
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Multithreading.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Operating Systems topic.
Search Terms
operating-systems, operating systems, operating, systems, process, management, multithreading
Related Operating Systems Topics