Information Technology Reference
In-Depth Information
calls/returns from procedures one after another.
A multi-threaded program is a generalization of the same basic programming
model. Each individual thread follows a single sequence of steps as it executes
statements, iterates through loops, calls/returns from procedures, etc. However,
a program can now have several such threads executing at the same time.
Uses for threads. A programmer|whether programming the operating sys-
tem or an outside application|uses threads to address three issues:
Program structure: Expressing logically concurrent tasks. Pro-
grams often interact with or simulate real world applications that have
concurrent activities. Threads allow the programmer to express an appli-
cation's concurrency by writing each concurrent task as a separate thread.
Example: Threads for program structure. In our Earth Visual-
izer application, threads allow different activities—updating the screen,
fetching additional data, and receiving new user inputs—to run in at the
same time. For example, we do not want to freeze the user interface
widgets while we fetch data and redraw the screen.
Programmers are used to thinking sequentially, and threads allow them
to write the code for each task as a separate, sequential program rather
than requiring them to write one program that somehow interleaves all of
these activities. Although one could imagine manually writing a program
that interleaves these activities (e.g., draw a few pixels on the screen,
then check to see if the user has moved the mouse, then check to see
if new image data have arrived on the network, . . . ), using threads can
make things much simpler.
Performance: Exploiting multiple processors. If programs can use
multiple processors to do their processing in parallel, they can run faster|
doing the same work in less time or doing more work in the same time.
Today, a server may have over a dozen processors, a desktop or laptop
machines may include two to eight processor cores, and even some cell
phones are multicore machines. Looking forward, Moore's law makes it
likely that the number of processors per system will continue to increase.
An important property of the threads abstraction is that the number of
threads used by the program need not match the number of processors in
the hardware on which it is running. The operating system transparently
switches which threads are running on which processors.
Example: Threads for parallel processing. If our Earth Visualizer
application runs on an 8-processor machine, the programmer might par-
allelize the demanding job of rendering different portions of the image
on the screen across seven threads. Then, the operating system might
run those seven rendering threads on seven processors and run the re-
maining threads on the remaining processor to update the on-screen
Search WWH ::




Custom Search