Model-oriented software architecture 2-concurrent and networked object model Reading Notes (12)-strategized locking

Source: Internet
Author: User

4.2 strategized locking)

1. Problem

Components running in multi-threaded environments must protect their critical section from concurrent access by clients. The integration of synchronization mechanisms and component functions requires the following two mandatory conditions:

1) different applicationsProgramDifferent synchronization policies may be required, such as mutex, read/write locks, or information lights. Therefore, you can customize the synchronization mechanism of components according to the specific application requirements.

2) It is easy to add new features and fix hidden errors. In particular, to avoid "version confusion", these changes should always be automatically and consistently applied to all members of the component series.

 

2. Solution

Change the synchronization feature of a component to a "pluggable" type. In this way, parameterize the synchronization feature of the component. Each type of synchronization involves specific synchronization policies, including mutex, read/write locks, traffic signals, or "empty" locks. The pluggable instances are defined as objects contained in the component. This component can use these objects to effectively synchronize their methods.

 

3. Implementation

1) define the interface and Implementation of the component without considering the synchronization feature of the component.

2) strategize the locking mechanism. Many components have relatively simple synchronization features and can use common locking policies, such as mutex and traffic signals, to implement these synchronization features. The synchronous characteristics can be strategized using Polymorphism or parameterized types in a unified manner. Generally, polymorphism should be used if the locking policy is not known until running. On the contrary, if the locking policy is known during compilation, the parameterized type should be used. Parameterization types have high operation efficiency, while polymorphism has the scalability potential for running.

In the usage of bounded locking, A strategized lock includes two sub-activities:

2.1) define an abstract interface for the locking mechanism. To enable components to use different locking mechanisms, all specific implementations of these mechanisms should use abstract interfaces with common features for obtaining and Releasing locks Based on Polymorphism or parameterized types.

2.2) define a sentinel class that uses synchronous features to strategize it using the bounded locking method. This design follows the policy pattern. Among them, the Sentinel class acts as a context with a special lock, and the specific lock provides a policy.

3) Update the interface and Implementation of the component. After strategizing the synchronization mechanism, components can use these mechanisms to explicitly obtain or release a lock, or use the defined sentinel class to protect critical zones.

4) modify the component implementation to avoid deadlocks and remove unnecessary lock overhead. If there is a method call between components, developers must carefully design their component implementations to avoid self-deadlock and unnecessary synchronization overhead.

5) define a set of locking policies with unified interfaces. These policies can support the specific Concurrent Design of various applications.

 

4. Conclusion

Advantages:

1) increased flexibility and personalization. It is easy to configure and customize a component for a specific concurrency model, because the synchronization feature of the component is strategized. If there is no suitable locking policy available for a new concurrency model, the existingCodeTo expand the new lock policy.

2) reduce the cost of component maintenance. Using the strategized locking mode, it is easy to improve components and implicit error correction, because there is only one implementation for various concurrency models, rather than each concurrency model corresponds to an independent implementation. The concentrated approach helps reduce version confusion.

3) improve reusability. Components implemented in this mode do not depend much on the specific synchronization mechanism.

 

Disadvantages:

1) Forcible locking.

2) Over-engineering ).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.