Information Technology Reference
In-Depth Information
Deja vu all over again?
Threads are widely used and modern programming languages often make it easy to
use threads. You may have programmed with threads before or may have taken classes
that talk about using threads. What is new here?
The discussions in this chapter and the next are designed to make sense even if you
have never seen threads before. If you have seen threads before, great! But we think
you will find the discussions useful.
Beyond describing the basics threads abstractions, we emphasize two points in our
discussions in this chapter and the next the next one.
Implementation. We will describe how operating systems implement threads
both for their own use and for use by user-level applications. It is important to
understand how threads really work so that you can understand their costs and
performance characteristics and use them effectively.
Practice. We will present a methodology for writing correct multi-threaded pro-
grams. Concurrency is increasingly important in almost all significant program-
ming projects, but writing correct multi-threaded programs requires much more
care and discipline than writing correct single-threaded programs. That said, fol-
lowing a few simple rules that we will describe can greatly simplify the process of
writing robust multi-threaded code.
Multithreaded programming has a reputation for being difficult, and we believe the ideas
in this chapter and the next can help almost anyone become better at programming with
threads.
Physical Reality
Programmer Abstraction
Threads
1 2 3 4 5
1 2 3 4 5
Ready
Threads
Processor
1
2
3
4
5
1 2
Running
Threads
Figure4.2: Threads virtualize the processor, providing the illusion that the
system has an infinite number of processors. Then, programmers can create
as many threads as they need, and each thread runs on a virtual processor.
In reality, of course, a machine only has a finite number of processors, and it
is the operating system's job to transparently multiplex threads on the actual
processors.
 
Search WWH ::




Custom Search