Model-oriented software architecture 2-concurrent and networked object model Reading Notes (13)-thread security interface and double check lock Optimization

Source: Internet
Author: User

4.3 thread-safe Interface)

1. Problem

Multi-threaded components generally include multiple public-accessible interface methods and private methods that can change the component status. To avoid competition, you can use the internal lock of a component to call the serialized interface method to access its status. Although this design method works well when each method is self-contained. However, component methods may call each other to complete computing tasks. In this case, the following mandatory conditions in a multi-threaded component have not been resolved, and they use the design method for calling the method between the wrong components:

1) The thread security component should avoid "self-deadlock ". An automatic deadlock occurs when a component method acquires a non-recursive lock in the component and calls another component method that tries to obtain the same lock.

2) to avoid competition for component states, thread-safe components should have the minimum lock overhead. If a recursive component lock is selected to avoid the self-deadlock problem described above, the unnecessary overhead is generated because the lock is obtained and released multiple times by calling methods between components.

 

2. Solution

Initialize all components that process method calls between components according to the following two conventions:

1) interface method check. All interface methods (such as C ++ public methods) should only obtain/release the component lock and perform synchronization check on the "edge" of the component. After obtaining a lock, the interface method is immediately forwarded to an implementation method, which implements the actual method function. After the implementation method is returned, the interface releases the lock before returning the control to the caller.

2) implement method trust. Only interfaces that call methods (such as C ++ private or protection methods) can complete their tasks. Therefore, they believe that they will be called only after the lock is obtained, and they should not acquire or release the lock themselves. The implementation method should not call the interface method "up" because the interface needs to obtain the lock.

 

3. Implementation

1) determine the interface and corresponding implementation methods. The interface method defines the public API of the component and defines an implementation method for each interface method.

2) programming interfaces and implementation methods.

 

4. Conclusion

Advantages:

1) Improved robustness. Because the inter-component method is called, this mode can avoid self-deadlock.

2) improved performance. This mode ensures that the lock is obtained or released only when necessary.

3) Simplified software. Separate the locking and feature issues to simplify the locking and feature.

 

Disadvantages:

1) Add indirect and additional methods.

2) potential deadlocks.

3) potential misuse.

4) potential overhead.

 

 

4.4 double-checked locking Optimization)

1. Problem

The serialization locking method is not suitable for objects or components that require initialization only once, such as the critical section in singleton.CodeIt must be executed once in the initialization phase. However, each method call to singleton needs to obtain and release the mutex lock, which increases overhead. To avoid these overheadProgramYou may switch to the global variable instead of the single-piece mode. However, this solution has two disadvantages:

· Poor portability. Generally, the order in which global objects defined in different files are constructed is not specified.

· A waste of resources. Because global variables are generated even if no global variables are used.

 

2. Solution

A flag is introduced to determine whether to execute the critical section before obtaining a lock that guarantees the critical section. If you do not need to execute this code, skip this critical section to avoid unnecessary locks and unlocking. Add the same flag in the critical section to check whether resources need to be initialized after entering the critical section, thus avoiding multiple Resource Initialization.

 

3. Implementation

1) determine the critical section to be executed only once. Operations (such as initialization logic) performed in this critical section are only performed once in the program.

2) Implement the locking logic. The lock logic serializes the access to the critical code that is executed only once.

3) implement the first-time-in sign. Indicates whether the critical section has been executed.

 

4. Conclusion

Advantages:

1) Minimize the lock overhead.

2) prevent competition conditions.

 

Disadvantages:

1) Non-atomic pointers or integrated value assignment semantics.

2) multi-processor cache consistency.

3) additional mutex usage.

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.