Multithreading is a programming and execution model that allows multiple threads to exist within a single process, executing concurrently. Each thread represents a separate path of execution, enabling better responsiveness, resource utilization and parallelism, especially in modern multiprocessor systems.
Note: Multithreading is widely used in applications like web servers, interactive applications and high-performance computing where concurrent execution is essential.
Note: Multithreading is widely used in applications like web servers, interactive applications and high-performance computing where concurrent execution is essential.
Operating systems support threads through different threading models, which determine how threads are created, managed and mapped to CPU execution:
Managed by a user-level library, not the OS.
- Greater flexibility and control: Programmers can customize scheduling.
- Portability: Works across multiple operating systems.
- Faster context switching: Switching between ULTs happens in user space.
- Blocking system calls: If one thread blocks, the entire process is blocked.
- Limited parallelism: Cannot utilize multiple CPUs effectively since the kernel is unaware of user threads.
Managed and scheduled directly by the operating system.
- True parallelism: Can run on multiple CPUs simultaneously.
- Better scalability: OS can efficiently schedule threads.
- I/O efficiency: Other threads can continue if one thread blocks.
- Less flexible and portable: Managed by the OS, harder to customize.
- Higher overhead: Thread creation and context switching involve system calls.
- Combines ULT and KLT advantages.
- Examples: Solaris and some modern OS use a two-level model, where user threads are mapped to kernel threads.
- Flexibility, control and efficient parallelism.
- Reduces blocking issues and improves concurrency.
- More complex to implement.
- Requires more system resources.

- Multiple user threads map to multiple kernel threads.
- If one thread blocks, others can continue.
- Provides high concurrency and is the most efficient model.

- Multiple user threads map to a single kernel thread.
- Blocking in one thread blocks the entire process.
- Efficient user-level management but poor multiprocessing utilization.

- Each user thread maps to a unique kernel thread.
- Multiple threads can run on multiple processors.
- Blocking in one thread does not affect others.
- Overhead is higher because each user thread requires a corresponding kernel thread.
Thread libraries provide APIs for creating and managing threads.
- User-level library: Runs entirely in user space; fast and flexible.
- Kernel-level library: Supported by the OS; better parallelism and fault tolerance.
- POSIX Pthreads: Can be implemented as ULT or KLT.
- Windows Threads: Kernel-level library.
- Java Threads: Typically built on kernel threads in modern JVMs.
- Minimized Context Switching Time: Switching threads is faster than switching processes.
- Concurrency: Multiple instruction sequences execute within a single process.
- Resource Efficiency: Threads share memory and resources of their parent process.
- Scalability on Multiprocessors: Threads can run on multiple CPUs, improving throughput.
- Responsiveness: Interactive applications remain responsive even when performing heavy tasks.
- Complexity: Threaded code can be harder to write and debug.
- High Management Overhead: Managing multiple threads may be costly for simple tasks.
- Risk of Deadlocks and Race Conditions: Improper synchronization may cause concurrency issues.
- Debugging Difficulty: Errors in multithreaded programs are often subtle and hard to reproduce.