Information Technology Reference
In-Depth Information
Threads: A core abstraction for concurrency. This chapter will focus
on the powerful concurrency abstraction of threads. Threads let us define a set
of tasks that should run at the same time, but they let us write the code for
each task as if the task were standard, sequential code.
Threads are used within the operating system and within user-level pro-
cesses.
Example: Threads in an operating system. For example an operating sys-
tem kernel may have one thread that writes modified file blocks from memory
to disk, another thread that encrypts (or decrypts) blocks as they are written
to (or read from) an encrypted disk, another thread that monitors system load
and manages the processor's power-saving features, and another thread that
scans the cache of disk blocks and frees old entries.
Example: Threads in an application. Consider an Earth Visualizer appli-
cation similar to Google Earth ( http://earth.google.com/ ) . This application
allows a user to virtually fly anywhere in the world, to see aerial images at dif-
ferent resolutions, and to view other information associated with each location.
A key part of the design is that the user's controls are always operable so that
as the user mouses to a new location, the image is redrawn in the background
at successively better resolutions while the program continues to allow the the
user to adjust the view, to select additional information about the location for
display, or to enter search terms.
To implement this application, as Figure 4.1 illustrates the programmer might
write code to draw a portion of the screen, code to draw user interface (UI)
widgets and process user inputs, and code to fetch higher resolution images
for newly-visible areas. In a sequential program, these functions would run
in turn. With threads, these can run concurrently so that the user interface
is responsive even while new data is being fetched and the screen is being
redrawn
Chapter overview.
The rest of this chapter discusses three topics in detail:
Thread abstraction. What are threads and what do they do?
Thread internals.
What building blocks are needed so that we can
construct threads?
Thread implementation.
Given these building blocks, how can we
implement threads?
4.1
Threads: Abstraction and interface
As Figure 4.2 illustrates, threads virtualize the processor, providing the illusion
that programs run on machines with an infinite number of virtual processors.
 
Search WWH ::




Custom Search