Information Technology Reference
In-Depth Information
modularity of object oriented programming to multi-threaded programs: as
Figure 5.1 illustrates, a multi-threaded program is build using shared objects
and a set of threads that operate on them.
Shared objects are objects that can safely be accessed by multiple threads.
Denition: Shared objects
All shared state in a program|including variables allocated on the heap (e.g.,
objects allocated with malloc() or new() ) and static, global variables|should
be encapsulated in one or more shared objects.
Shared objects extend traditional object-oriented programming in which ob-
jects hide their implementation details behind a clean interface. In the same
way, shared objects hide the details of synchronizing the actions of multiple
threads behind a clean interface. The threads using shared objects just need
to understand the interface, they don't need to know how the shared object
internally handles synchronization.
Just like regular objects, the shared objects are completely general|programmers
implement shared objects for whatever modules, interfaces, and semantics their
application needs. Each shared object's class denes a set of public methods
on which threads operate. Then, to assemble the overall program out of these
shared objects, each thread executes some \main loop" written in terms of ac-
tions on public methods of shared objects.
Note that since the shared objects encapsulate the program's shared state,
the main-loop code that denes a thread's high-level actions doesn't concern
itself with the details of synchronization. The programming model thus looks
very much like it does for single-threaded code.
Implementing shared objects. Of course, internally the shared objects
must handle the details of synchronization.
As Figure 5.2 shows, shared ob-
jects are implemented in three layers.
Shared objects. As in standard object-oriented programming, the shared
objects define the application-specific logic and hide the internal imple-
mentation details. Externally, shared objects appear essentially the same
as objects you define for single-threaded programs.
Synchronization variables. Rather than trying to implement shared
objects directly with carefully interleaved atomic loads and stores, shared
objects include synchronization variables as member variables. Synchro-
nization variables are stored in memory just like any other object, so they
can be included in any data structure.
Synchronization variables are instances of carefully designed classes that
Denition:
Synchronization
variables
provide broadly-useful primitives for synchronization. In particular, we
build shared objects using two types of synchronization variables: locks
and condition variables. We define these and describe how to construct
them later.
 
Search WWH ::




Custom Search