A thread is a basic unit of CPU utilization; it comprises a thread ID, a program counter, a register set, and a stack. It shares with other threads belonging to the same process its code section, data section, and other operating-system resources, such as open files and signals. A traditional (or heavyweight) process has a single thread of control.
Many software packages that run on modern desktop PCs are multithreaded. An application typically is implemented as a separate process with several threads of control.A web browser might have one thread display images or text while another thread retrieves data from the network, for example. A
word processor may have a thread for displaying graphics, another thread.
The benefits of multithreaded programming can be broken down into four major categories:
- Responsiveness. Multithreading an interactive application may allow a program to continue running even if part of it is blocked or is performing a lengthy operation, thereby increasing responsiveness to the user. For instance, a multithreaded web browser could still allow user interaction in one thread while an image was being loaded in another thread.
- Resource sharing. By default, threads share the memory and the resources of the process to which they belong. The benefit of sharing code and data is that it allows an application to have several different threads of activity within the same address space.
- Economy. Allocating memory and resources for process creation is costly.Because threads share resources of the process to which they belong, it is more economical to create and context-switch threads. Empirically gauging the difference in overhead can be difficult, but in general it is much more time consuming to create and manage processes than threads. In Solaris, for example, creating a process is about thirty times slower than is creating a thread, and context switching is about five times slower.
- Utilization of multiprocessor architectures. The benefits of multithreading can be greatly increased in a multiprocessor architecture, where threads may be running in parallel on different processors. A singlethreaded process can only run on one CPU, no matter how many are available. Multithreading on a multi-CPU machine increases concurrency.