A thread in computer science is short for a thread of execution. Threads are a way for a program to split itself into two or more simultaneously (or pseudo-simultaneously) running tasks. Threads and processes differ from one operating system to another, but in general, the way that a thread is created and shares its resources is different from the way a process does.
Multiple threads can be executed in parallel on many computer systems. This multithreading generally occurs by time slicing, wherein a single processor switches between different threads, in which case the processing is not literally simultaneous, for the single processor is only really doing one thing at a time. This switching can happen so fast as to give the illusion of simultaneity to an end user. For instance, a typical PC today contains only one processor, but you can run multiple programs at once, such as a word processor alongside an audio playback program; though the user experiences these things as simultaneous, in truth, the processor is quickly switching back and forth between these separate threads. On a multiprocessor system, threading can be achieved via multiprocessing, wherein different threads can run literally simultaneously on different processors.
Many modern operating systems directly support both time-sliced and multiprocessor threading with a process scheduler. The operating system kernel allows programmers to manipulate threads via the system call interface. Some implementations are called a kernel thread, whereas a lightweight process is a specific type of kernel thread that shares the same state and information.
Absent that, programs can still implement threading by using timers, signals, or other methods to interrupt their own execution and hence perform a sort of ad hoc time-slicing. These are sometimes called user-space threads.
An unrelated use of the term thread is for threaded code, which is a form of code consisting entirely of subroutine calls, written without the subroutine call instruction, and processed by an interpreter or the CPU. Two threaded code languages are Forth and early B programming languages.
|
Contents
- 1 Threads compared with processes
- 2 Processes, threads, and fibers
- 2.1 Thread and fiber issues
- 2.2 Relationships between processes, threads, and fibers
- 3 Implementations
- 3.1 Kernel-level implementation examples
- 3.2 User-level implementation examples
- 3.3 Hybrid implementation examples
- 4 Example program
- 5 See also
- 6 References
- 7 External links
|
Threads compared with processes
Threads are distinguished from traditional multi-tasking operating system processes in that processes are typically independent, carry considerable state information, have separate address spaces, and interact only through system-provided inter-process communication mechanisms. Multiple threads, on the other hand, typically share the state information of a single process, and share memory and other resources directly. Context switching between threads in the same process is typically faster than context switching between processes. Systems like Windows NT and OS/2 are said to have "cheap" threads and "expensive" processes, while in other operating systems there is not so great a difference.
Multithreading is a popular programming and execution model that allows multiple threads to exist within the context of a single process, sharing the process' resources but able to execute independently. The threaded programming model provides developers with a useful abstraction of concurrent execution. However, perhaps the most interesting application of the technology is when it is applied to a single process to enable parallel execution on a multiprocessor system.
This advantage of a multi-threaded program allows it to operate faster on computer systems that have multiple CPUs, CPUs with multiple cores, or across a cluster of machines. This is because the threads of the program naturally lend themselves for truly concurrent execution. In such a case, the programmer needs to be careful to avoid race conditions, and other non-intuitive behaviors. In order for data to be correctly manipulated, threads will often need to rendezvous in time in order to process the data in the correct order. Threads may also require atomic operations (often implemented using semaphores) in order to prevent common data from being simultaneously modified, or read while in the process of being modified. Careless use of such primitives can lead to deadlocks.
Operating systems generally implement threads in one of two ways: preemptive multithreading, or cooperative multithreading. Preemptive multithreading is generally considered the superior implementation, as it allows the operating system to determine when a context switch should occur. Cooperative multithreading, on the other hand, relies on the threads themselves to relinquish control once they are at a stopping point. This can create problems if a thread is waiting for a resource to become available. The disadvantage to preemptive multithreading is that the system may make a context switch at an inappropriate time, causing priority inversion or other bad effects which may be avoided by cooperative multithreading.
Traditional mainstream computing hardware did not have much support for multithreading as switching between threads was generally already quicker than full process context switches. Processors in embedded systems, which have higher requirements for real-time behaviors, might support multithreading by decreasing the thread switch time, perhaps by allocating a dedicated register file for each thread instead of saving/restoring a common register file. In the late 1990s, the idea of executing instructions from multiple threads simultaneously has become known as simultaneous multithreading. This feature was introduced in Intel's Pentium 4 processor, with the name Hyper-threading.
Processes, threads, and fibers
The concept of a process, thread, and fiber are interrelated by a sense of "ownership" and of containment.
A process is the "heaviest" unit of kernel scheduling. Processes own resources allocated by the operating system. Resources include memory, file handles, sockets, device handles, and windows. Processes do not share address spaces or file resources except through explicit methods such as inheriting file handles or shared memory segments, or mapping the same file in a shared way. Processes are typically pre-emptively multitasked. However, Windows 3.1 and older versions of Mac OS used co-operative or non-preemptive multitasking.
A thread is the "lightest" unit of kernel scheduling. At least one thread exists within each process. If multiple threads can exist within a process, then they share the same memory and file resources. Threads are pre-emptively multitasked if the operating system's process scheduler is pre-emptive. Threads do not own resources except for a stack and a copy of the registers including the program counter.
In some situations, there is a distinction between "kernel threads" and "user threads" -- the former are managed and scheduled by the kernel, whereas the latter are managed and scheduled in userspace. In this article, the term "thread" is used to refer to kernel threads, whereas "fiber" is used to refer to user threads. Fibers are co-operatively scheduled: a running fiber must explicitly "yield" to allow another fiber to run. A fiber can be scheduled to run in any thread in the same process.
Thread and fiber issues
Typically fibers are implemented entirely in userspace. As a result, context switching between fibers in a process is extremely efficient: because the kernel is oblivious to the existence of fibers, a context switch does not require any interaction with the kernel at all. Instead, a context switch can be performed by locally saving the CPU registers used by the currently executing fiber and loading the registers required by the fiber to be executed. Since scheduling occurs in userspace, the scheduling policy can be more easily tailored to the requirements of the program's workload.
However, the use of blocking system calls in fibers can be problematic. If a fiber performs a system call that blocks, the other fibers in the process are unable to run until the system call returns. A typical example of this problem is when performing I/O: most programs are written to perform I/O synchronously. When an I/O operation is initiated, a system call is made, and does not return until the I/O operation has been completed. In the intervening period, the entire process is "blocked" by the kernel and cannot run, which starves other fibers in the same process from executing.
A common solution to this problem is providing an I/O API that implements a synchronous interface by using non-blocking I/O internally, and scheduling another fiber while the I/O operation is in progress. Similar solutions can be provided for other blocking system calls. Alternatively, the program can be written to avoid the use of synchronous I/O or other blocking system calls.
Win32 supplies a fiber API. SunOS 4.x implemented "light-weight processes" or LWPs as fibers known as "green threads". SunOS 5.x and later, NetBSD 2.x, and DragonFly BSD implement LWPs as threads as well.
The use of kernel threads simplifies user code by moving some of the most complex aspects of threading into the kernel. The program doesn't need to schedule threads or explicitly yield the processor. User code can be written in a familiar procedural style, including calls to blocking APIs, without starving other threads. However, since the kernel may switch between threads at any time, kernel threading usually requires locking that wouldn't be necessary otherwise. Bugs caused by incorrect locking can be very subtle and hard to reproduce. Kernel threading also has performance limits. Each time a thread starts, blocks, or exits, the process must switch into kernel mode, then back into user mode. This context switch is fairly quick, but programs that create many short-lived threads can suffer a performance hit. Hybrid threading schemes are available which provide a balance between kernel threads and fibers.
In most cases, the work required to use fibers is not justified, as most modern operating systems provide efficient support for threads.
Relationships between processes, threads, and fibers
The operating system creates a process for the purpose of running a program. Every process has at least one thread. On some operating systems, a process can have more than one thread. A thread can use fibers to implement cooperative multitasking to divide the thread's CPU time for multiple tasks. Generally, this is not done because threads are cheap, easy, and well-implemented in modern operating systems.
Processes are used to run an instance of a program. Some programs like word processors are designed to have only one instance of themselves running at the same time. Sometimes, such programs just open up more windows to accommodate multiple simultaneous use. After all, you can go back and forth between five documents, but you can only edit one of them at a given instance.
Other programs like command shells maintain a state that you want to keep separate. Each time you open a command shell in Windows, the operating system creates a process for that shell window. The shell windows do not affect each other. Some operating systems support multiple users being logged in simultaneously. It is typical for dozens or even hundreds to thousands of people to be logged into some Unix systems. Other than the possible sluggishness of an overloaded computer, the individual users are (usually) blissfully unaware of each other. If Bob runs a program, the operating system creates a process for it. If Alice then runs the same program, the operating system creates another process to run Alice's instance of that program. So if Bob's instance of the program crashes, Alice's instance does not. In this way, processes protect users from failures being experienced by other users. Because new processes created by a user also inherits its permissions, it also allows to maintain security between the users.
However, there are times when a single process needs to do multiple things concurrently. The quintessential example is a program with a graphical user interface (GUI). The program must repaint its GUI and respond to user interaction even if it is currently spell-checking a document or playing a song. For situations like these, threads are used. Another example is the use of threads by web servers. When a user visits a web site, a web server will often use a thread to serve the page to that user. If another user visits the site while the previous user is still being served, the web server can serve the second visitor by using a different thread. Thus, the second user does not have to wait for the first visitor to be served.
Threads allow a program to do multiple things concurrently. Since the threads, spawned by a program, share the same address space, one thread can modify data that is used by another thread easily and efficiently. This is both a good and a bad thing. It is good because it facilitates easy communication between threads and allows high performance. It can be bad because a poorly written program may cause one thread to inadvertently overwrite data being used by another thread. The sharing of a single address space between multiple threads is one of the reasons that multithreaded programming is usually considered to be more difficult and error-prone than programming a single-threaded application.
There are other potential problems as well such as deadlocks, livelocks, and race conditions. However, all of these problems are concurrency issues and as such affect multi-process and multi-fiber models as well.
There are situations where creating other processes remains useful, such as when an application requires some operations to be executed under the privileges of another user (privilege separation), or that the operations are complex enough and that the application needs to make sure to avoid resource leaks. An example can be command shells, FTP or web servers which need to execute operations under various real system users (as opposed to virtual users). These being traditional processes, they independently have the ability to spawn threads as needed.
Implementations
There are many different and incompatible implementations of threading. These include both kernel-level and user-level implementations.
Note that fibers can be implemented without operating system support, although some operating systems or libraries provide explicit support for them. For example, recent versions of Microsoft Windows (Windows NT 3.51 SP3 and later) support a fiber API for applications that want to gain performance improvements by managing scheduling themselves, instead of relying on the kernel scheduler (which may not be tuned for the application). Microsoft SQL Server 2000's user mode scheduler, running in fiber mode, is an example of doing this.
Kernel-level implementation examples
- Light Weight Kernel Threads in various BSDs
- M:N threading (in BSDs)
- Native POSIX Thread Library for Linux, an implementation of the POSIX Threads (pthreads) standard
- Apple Multiprocessing Services (in version 2.0 and later, uses the built-in nanokernel in Mac OS 8.6 and later.
User-level implementation examples
- GNU Portable Threads [1]
- FSU Pthreads
- Apple Computer's Thread Manager
- REALbasic's cooperative threads
Hybrid implementation examples
- Scheduler activations used by the NetBSD native POSIX threads library implementation (an N:M model as opposed to a 1:1 kernel or userspace implementation model)
Example program
This is an example of a simple multi-threaded program written in the Java programming language. The program calculates prime numbers until the user types the word "stop". Then the program prints how many prime numbers it found and exits. This example demonstrates how threads can access the same variable while working asynchronously. This example also demonstrates a simple "race condition". The thread printing prime numbers continues to do so for a short time after the user types "stop". Of course, this problem is easily corrected using standard programming techniques.
import java.io.*;
public class Example implements Runnable
{
static Thread threadCalculate;
static Thread threadListen;
long totalPrimesFound = 0;
public static void main (String[] args)
{
Example e = new Example();
threadCalculate = new Thread(e);
threadListen = new Thread(e);
threadCalculate.start();
threadListen.start();
}
public void run()
{
Thread currentThread = Thread.currentThread();
if (currentThread == threadCalculate)
calculatePrimes();
else if (currentThread == threadListen)
listenForStop();
}
public void calculatePrimes()
{
int n = 1;
while (true)
{
n++;
boolean isPrime = true;
for (int i = 2; i < n; i++)
if ((n / i) * i == n)
{
isPrime = false;
break;
}
if (isPrime)
{
totalPrimesFound++;
System.out.println(n);
}
}
}
private void listenForStop()
{
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
String line = "";
while (!line.equals("stop"))
{
try
{
line = input.readLine();
}
catch (IOException exception) {}
}
System.out.println("Found " + totalPrimesFound +
" prime numbers before you said stop");
System.exit(0);
}
}
See also
- Chip-level multiprocessing
- Corn programming language
- Simultaneous multithreading
- List of multi-threading libraries
- clone()
- Communicating sequential processes
- Completion port
- Computer multitasking
- exit
- fork
- Lock-free and wait-free algorithms
- Message passing
- Priority inversion
- Protothreads
- Synchronization
- Thread safety
- Thread pool pattern
References
- David R. Butenhof: Programming with POSIX Threads, Addison-Wesley, ISBN 0-201-63392-2
- Bradford Nichols, Dick Buttlar, Jacqueline Proulx Farell: Pthreads Programming, O'Reilly & Associates, ISBN 1-56592-115-1
- Charles J. Northrup: Programming with UNIX Threads, John Wiley & Sons, ISBN 0-471-13751-0
- Mark Walmsley: Multi-Threaded Programming in C++, Springer, ISBN 1-85233-146-1
- Paul Hyde: Java Thread Programming, Sams, ISBN 0-672-31585-8
- Bill Lewis: Threads Primer: A Guide to Multithreaded Programming, Prentice Hall, ISBN 0-13-443698-9
- Steve Kleiman, Devang Shah, Bart Smaalders: Programming With Threads, SunSoft Press, ISBN 0-13-172389-8
- Pat Villani: Advanced WIN32 Programming: Files, Threads, and Process Synchronization, Harpercollins Publishers, ISBN 0-87930-563-0
- Jim Beveridge, Robert Wiener: Multithreading Applications in Win32, Addison-Wesley, ISBN 0-201-44234-5
- Thuan Q. Pham, Pankaj K. Garg: Multithreaded Programming with Windows NT, Prentice Hall, ISBN 0-13-120643-5
- Len Dorfman, Marc J. Neuberger: Effective Multithreading in OS/2, McGraw-Hill Osborne Media, ISBN 0-07-017841-0
- Alan Burns, Andy Wellings: Concurrency in ADA, Cambridge University Press, ISBN 0-521-62911-X
- Uresh Vahalia: Unix Internals: the New Frontiers, Prentice Hall, ISBN 0-13-101908-2
- Alan L. Dennis: .Net Multithreading , Manning Publications Company, ISBN 1-930110-54-5
- Tobin Titus, Fabio Claudio Ferracchiati, Srinivasa Sivakumar, Tejaswi Redkar, Sandra Gopikrishna: C# Threading Handbook, Peer Information Inc, ISBN 1-86100-829-5
- Tobin Titus, Fabio Claudio Ferracchiati, Srinivasa Sivakumar, Tejaswi Redkar, Sandra Gopikrishna: Visual Basic .Net Threading Handbook, Wrox Press Inc, ISBN 1-86100-713-2
External links
- Real World Tech article by Paul DeMone - Explaining different types of multithreading, hardware implementation requirements and the impact on software.
- Ars Technica article about multithreading, etc
- Forum
- Answers to frequently asked questions for comp.programming.threads
- The C10K problem
- System support for scalable network servers
- Article "The Free Lunch Is Over: A Fundamental Turn Toward Concurrency in Software" by Herb Sutter
- Article "The Problem with Threads" by Edward A. Lee
- DragonFly - Light Weight Kernel Threading Model
- Java(TM) and Solaris(TM) Threading
- An Implementation of Scheduler Activations on the NetBSD Operating System
- POSIX Threads specification
- Sun Studio multithreading tools for Solaris OS and Linux
Categories: Operating system technology | Concurrent computing