A thread is the smallest unit of programmed instructions that can be managed independently and processing that can be performed by an operating system. A thread is also called a lightweight process. Multiple threads can be existing within the same process that shares common resources such as memory. If a process has multiple threads of control, it can perform more than one task at a time. Threads within a process share the same virtual memory space but each has a separate stack, and possibly “thread-local storage” if implemented. They are lightweight because a context switch is simply a case of switching the stack pointer and program counter and restoring other registers, whereas a process context switch involves switching the MMU context as well.
For example, a web browser can have a thread to display images or text while another thread retrieves data from the network. Also in a word processor, a background thread may check spelling and grammar while a foreground thread processes user input (keystrokes), while yet a third thread loads images from the hard drive, and a fourth does periodic automatic backups of the file being edited. There are different types of threads in process and management.
Advantages of using Threads
- They are easy and inexpensive to create. They are 10 times as faster to create than process (studies done by Mach developers).
- It takes less time to terminate the thread than process.
- The use of thread provides concurrency within the process.
- They minimize context switching time (i.e. switching between two threads within the same process is faster).
- They enhance efficiency in communication between different executing programs. (Threads within the same process share data and code memory which allows them to communicate without involving the kernel).
- Utilization of multiprocessor architectures to a greater scale and efficiency.
You may also like: Operation of Process