Chapter 4
Synchronization Patterns
“I will be the pattern of all patience.”
William Shakespeare
This chapter describes three patterns and one idiom that simplify locking in concurrent systems: Scoped Locking, Strategized Locking, Thread-Safe Interface, and Double-Checked Locking Optimization.
Developing multi-threaded applications is harder than developing sequential programs because an object can be manipulated concurrently by multiple threads, which may corrupt its internal state. Synchronization mechanisms, such as mutexes or semaphores [McK95], can help ensure objects are serialized correctly. This chapter presents three patterns and an idiom that provide solutions to problems related to synchronizing concurrent objects.
The first idiom and pattern address lock acquisition/release and locking strategies:
- The Scoped Locking C++ idiom (325) ensures that a lock is acquired automatically when control enters a scope and released automatically when control leaves the scope, regardless of the return path from the scope.
- The Strategized Locking design pattern (333) is a specialization of the Strategy pattern [GoF95] that parameterizes the synchronization mechanisms used in a component that protect its critical sections from concurrent access.
When implemented in C++ the Strategized Locking pattern often applies the Scoped Locking idiom. The other two patterns help improve the robustness and efficiency of synchronization mechanisms:
- The Thread-Safe Interface design pattern (345) ...
Get Pattern-Oriented Software Architecture, Volume 2, Patterns for Concurrent and Networked Objects now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.