Information Technology Reference
In-Depth Information
Network
Stage
Parse
Stage
Render
Stage
Figure6.1: A multi-stage server based on the ownership pattern. In the first
stage, one thread exclusively owns each network connection.
In later stages,
one thread is processing a given object at a time.
This advice is admittedly rather generic and perhaps obvious: of course
one should strive for elegant designs for both single- and multi-threaded code.
Nonetheless, we emphasize that the choices you make for your interfaces, ab-
stractions, and software architecture can dramatically affect the complexity or
feasibilty of your designs.
Ownership pattern
One common synchronization technique in large, multi-threaded programs is an
ownership design pattern in which a thread removes an object from a container
Denition: ownership
design pattern
and then may access the object without holding a lock because the program
structure guarantees that at most one thread owns an object at a time.
Example: Work queue. A single web page can contain multiple objects
including html frames, style sheets, and images. Consider a multi-threaded
web browser whose processing is divided into three stages: receiving an object
via the network, parsing the object, and rendering the object (see Figure 6.1.)
For the first stage, we have one thread per network connection, and for the
other stages we have several worker threads, each of which processes one
object at a time.
The work queues between stages coordinate object ownership. Objects in
the queues are not being accessed by any thread. When a worker thread in
the parse stage removes an object from the stage's work queue, it owns the
object and has exclusive access to it. When it is done parsing the object, it
puts the parsed object into the second queue and stops accessing it. A worker
thread from the render stage then removes it from the second queue, gaining
exclusive access to it to render it to the screen.
Search WWH ::




Custom Search