5.4 Synchronization: An Object-Oriented Approach

One of the advantages of object-oriented programming is the protection encapsulation provides for the data component of an object. Encapsulation can provide "object-access policies and usage guidelines" (Hughes & Hughes, 1997) for the user of the object. In the examples presented in this chapter, access policies were the responsibility of the user of the data. With objects and encapsulation, the responsibility has switched from the user of the data to the data itself. This approach creates data, not unlike functions, which are thread safe.

In order to accomplish this, the data (wherever possible) of the multithreaded application should be encapsulated using the C++ class or struct constructs. Then encapsulate the synchronization mechanism such as semaphores, read-write locks, and event mutexes . If the data or synchronization mechanisms are already objects, create an interface class for them. Lastly, combine the data object with the synchronization objects through inheritance or composition, to create data objects that are thread safe. This approach is discussed in detail in Chapter 11.



Parallel and Distributed Programming Using C++
Parallel and Distributed Programming Using C++
ISBN: 0131013769
EAN: 2147483647
Year: 2002
Pages: 133

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net