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.
