1. Introduction to Threads
A thread is the smallest unit of execution in a process. A process can have one or multiple threads running simultaneously.
💡 Why use threads?
- Faster execution (lightweight compared to processes).
- Efficient CPU utilization (multiple threads share the same process resources).
- Improves multitasking & responsiveness (e.g., running animations while processing user input).
2. Process vs. Thread
Feature | Process | Thread |
---|---|---|
Definition | A program in execution | A lightweight unit within a process |
Resource Sharing | Each process has separate memory & resources | Threads share memory & resources of the process |
Communication | Inter-Process Communication (IPC) required | Easier communication (shared memory) |
Creation Overhead | High (new process requires new memory & system resources) | Low (threads use existing process memory) |
Example | Running two different applications | Running multiple tasks in a web browser (loading, rendering, scrolling) |
✔ Example:
- Opening MS Word creates a process.
- Spell check, saving files, and text formatting run as threads inside the MS Word process.
3. Types of Threads
📌 1. User-Level Threads (ULT)
✅ Managed by user-space libraries, not the OS.
✅ Faster context switching since the OS is not involved.
✅ If one thread blocks, all threads in the process block.
✔ Example: Java Threads, POSIX Threads (Pthreads)
📌 2. Kernel-Level Threads (KLT)
✅ Managed by the Operating System (OS).
✅ Slower than User-Level Threads due to OS involvement.
✅ Can use multi-core processors efficiently.
✔ Example: Windows, Linux kernel threads.
4. Multithreading Models
Multithreading models describe how user threads are mapped to kernel threads.
Model | Description | Example OS |
---|---|---|
Many-to-One | Multiple user threads map to one kernel thread. | Solaris Green Threads |
One-to-One | Each user thread maps to one kernel thread. | Windows, Linux |
Many-to-Many | Multiple user threads map to multiple kernel threads. | Modern Linux, Solaris |
5. Benefits of Threads
✔ Faster Execution – No need for full process creation.
✔ Better CPU Utilization – Multiple threads can run on multiple cores.
✔ Efficient Memory Use – Threads share memory, reducing overhead.
✔ Improved Performance – Parallel execution speeds up tasks.
6. Example of Threads in Real-Life Applications
✔ Web Browser (Chrome, Firefox)
- One thread for UI Rendering
- One thread for Page Loading
- One thread for Downloading Files
✔ Text Editor (MS Word, Google Docs)
- One thread for Typing Input
- One thread for Auto-saving
- One thread for Spell Check
✔ Video Games
- One thread for Rendering Graphics
- One thread for Handling Input
- One thread for Game Physics
7. Conclusion
✔ Threads are lightweight execution units inside a process.
✔ They improve multitasking and CPU efficiency.
✔ User-Level Threads are faster, Kernel-Level Threads are more powerful.
✔ Multithreading is widely used in modern software like browsers, games, and OS kernels.