This pattern is really a shock for me. I met it in some projects before but I cannot tell why they code in this way. Now I do know the exact reason to apply this pattern.
The Thread-Safe Interface design pattern minimizes locking overhead and ensures that intra-component method calls do not incur 'self-deadlock' by trying to reacquire a lock that is held by the component already.
Structure all components that process intra-component method invocations according two design conventions:
Interface methods check: All interface methods, such as C++ public methods, should only acquire/release component lock(s), thereby performing synchronization checks at the 'border' of the component. After the lock is acquired, the interface method should forward immediately to an implementation method, which performs the actual method functionality. After the implementation method returns, the interface method should release the lock(s) before returning control to the caller.
Implementation methods trust: Implementation methods, such as C++ private and protected methods, should only perform work when called by interface methods. They therefore trust that they are called with the necessary lock(s) held and should never acquire or release lock(s). Implementation methods should also never call 'up' to interface methods, because these methods acquire lock(s).
According to the design conventions described above, you need to determine the interface and corresponding implementation methods. The interface methods define the public API of the component. For each interface method, define a corresponding implementation method.
It's really something, right?
线程安全接口设计模式
本文介绍了线程安全接口设计模式的应用及其实现原理。该模式通过特定的设计规范确保组件内部方法调用不会因尝试重新获取已持有的锁而导致自我死锁,从而减少锁的竞争开销。文中详细解释了接口方法和实现方法之间的区别与联系。
1229

被折叠的 条评论
为什么被折叠?



