3 Threads and Multithreading
Learn how processes and threads differ, what multithreading enables and risks, and how common threading models map user threads to kernel threads.
Processes and threads
A is a running program together with operating-system resources, such as a virtual address space and access to files. A is a sequence of execution within a . A may contain one or several.
Each has its own execution state, including a program counter, CPU registers, and stack. Threads in the same share the ’s code, global data, heap, and many resources. In POSIX systems, for example, threads share global memory and open file descriptors but have distinct stacks and IDs. The exact resources shared depend on the operating system and its interfaces.
Separate processes typically have separate address spaces, while threads in one share an address space. Each has its own execution state; each has its own registers, program counter, and stack. Separate processes often require interprocess communication (IPC) to communicate, whereas threads can communicate through shared variables.
Processes provide stronger separation: a ’s memory is generally protected from other processes. Threads can access shared memory, so a faulty write by one may affect its fellow threads. For example, a web browser might isolate work in separate processes, while one application might use several threads for independent tasks.
What enables
means using multiple threads within one . It can improve responsiveness when one waits for input or file access while another continues working. For example, a word processor can keep accepting keystrokes while another saves a document.
means tasks make progress during overlapping periods. On a single core, the operating system can interleave their execution. means tasks execute simultaneously, for example on separate processor cores. can support both, but creating threads does not guarantee that a program will run faster.
Sharing memory is convenient, but creates risks when threads access the same data. If two threads update the same value without coordination, their operations can overlap and produce an incorrect result; this is a . Locks and other synchronization tools coordinate access to shared data. Poor coordination can cause bugs such as deadlocks.
Threading models
Threading models describe how —threads managed by a program or runtime—map to , which the operating system can schedule. The mapping affects parallel execution and what happens when a blocks.
In the model, many map to one kernel . Managing threads in user space can be efficient, but a blocking system call may stop the sole kernel and prevent the other from running. Because only one kernel is available, the cannot run its threads in parallel on multiple cores.
In the model, each user maps to a kernel . The operating system can schedule different threads independently, including on multiple cores, and one blocking does not necessarily prevent others from running. The trade-off is that creating and managing many uses more system resources.
In the model, many are multiplexed over a set of . This allows a program to have more than while still letting the operating system schedule multiple . The model combines user-level flexibility with the potential for parallel execution, but requires a more complex implementation.
The is a variation of that also allows some to be bound to particular . These models describe general design choices; specific operating systems and runtimes differ in their implementations.