A thread is a basic unit of CPU utilization; it comprises a thread ID, a program counter, a register set, and a stack. A traditional process has a single thread of control. If a process has multiple threads of control, it can perform more than one task at a time. A thread shares with other threads belonging to the same process its code section, data section, and other operating-system resources.
Advantages of Thread
- Resource Sharing: Threads share the memory and the resources of the process to which they belong by default.
- Economy: It is more economical to create and context-switch threads
- Responsiveness: Multithreading increases the responsiveness of application by allowing a program to continue running even if part of it is blocked or is performing a long running operation.
- Scalability: Threading increases the parallelism of CPU by running the parallel threads on different processors, thus increasing the scalability.
Types of Thread
Threads are of two types:
- User Level Thread: User like programmer manages these threads
- Kernel Level Thread: Kernel manages these threads.
Multithreading model defines relationship between user level thread and kernel level thread. Models are of three types:
- Many-to-One model: Many-to-One model maps many user-level threads to one kernel thread.
- One-to-One model: It maps each user thread to a kernel thread.
Many-to-Many model: It multiplexes many user-level threads to a smaller or equal number of kernel threads.