SlideShare a Scribd company logo
Architectural Patterns
[PART 3]
(Synchronization idioms & Pattern)

based on
Pattern-Oriented Software Architecture, Patterns for Concurrent and
Networked Objects, Volume 2
by Douglas Schmidt, Michael Stal, Hans Rohnert and Frank Buschmann
Scoped Locking
It ensures that a lock is acquired automatically when control enters a scope and released
automatically when control leaves the scope.
Implementation 1. Define a guard class that acquires and releases a lock in its constructor and destructor
respectively.
class Thread_Mutex_Guard {
private:
Thread_Mutex *lock_; // Pointer to lock.
bool owner_; // Set to true when a lock is acquired
Thread_Mutex_Guard (const Thread_Mutex_Guard&);
// //disallow copy constructor and = operator
void operator= (const Thread_Mutex_Guard &);
public:
Thread_Mutex_Guard (Thread_Mutex &lock): lock_ (&lock), owner_ (false) {
lock_->acquire (); owner_ = true; }
~Thread_Mutex_Guard () { if (owner_) lock_->release (); }
};
Scoped Locking
2. call the thread_Mutex_Guard class object inside the class / function
Class test {
int test1(int index) {
Thread_Mutex_Guard guard (lock_);
if (/* condition */) {
// Do some more work ...
return true; } // End of scope releases the lock.
Return false ; // End of scope releases the lock.
}
Private :
Thread_Mutex lock_;
};

Mutex Lock is acquired and released automatically as control enters and leaves the test1()
method respectively.
Two specific functions acquire() and release() can be created inside the
Thread_Mutex_Guard class to acquire the lock and release the lock. In acquire(), it should
be verified if lock is already acquired. In release() should be verified if lock is already
released. This way if lock is already released , then release() in destructor can verify it.
Strategized Locking
The Strategized Locking technique parameterizes synchronization
mechanisms that protect a component's critical sections from concurrent
access.
To increase performance on large-scale multi-processor platforms, it may be
required to change the synchronization strategy to more efficient e.g. from
thread mutex to readers/writer lock which is time-consuming and error
prone.
Strategized Locking, the family of locking mechanisms becomes more
reusable and easier to apply across applications.
Implementation 1. Define an abstract interface for the locking mechanisms
2. Define a guard class and pass specific concrete lock object based on
requirement.
3. Update the component interface and implementation
Strategized Locking
# Define an abstract interface for the locking mechanisms
class Lock {
public:
// define methods.
virtual void acquire () = 0;
virtual void release () = 0;
};
# Define concrete classes for each kind of lock (e.g. concrete class for mutex lock)
class Thread_Mutex_Lock : public Lock {
public:
Virtual void acquire () { lock_.acquire (); }
Virtual void release () { lock_.release (); }
private:
Thread_Mutex mutexLock_; // Concrete lock type.

friend class Guard; // Define <test> as a friend so it can access <mutexLock_>
};
Strategized Locking
# Define guard class
class Guard {
public:
Guard (Thread_Mutex &lock): lock_ (&lock), owner_ (false) { lock_->acquire
(); owner_ = true; }
~Guard () { if (owner_) lock_->release (); }
private:
// Pointer to the lock.
Thread_Mutex *lock_;
bool owner_;
};
Strategized Locking
# Implement the interface
Polymorphic lock can be passed to the component either as a parameter in its
constructor or by adding a lock template parameter to the component declaration.
class test 1{
public:
// pass the Constructor the concrete lock object
test (Lock & mutexLock_) : lock_ (mutexLock_) {}; // lock passed as a parameter in its
constructor
void Function1(const char *pathname) {
Guard guard (lock_);
//Critical section
return;
}
private:
Thread_Mutex *lock_;
};
Thread-Safe Interface
This pattern ensures If a method that uses the Scoped Locking idiom, does
not call itself recursively to avoid self-deadlock.
Double-Checked Locking Optimization This technique avoids race conditions when accessing and modifying shared
resources by concurrent application during program execution
Implementation –
• All interface methods, (e.g. C++ public methods) should only
acquire/release component lock(s).
•

Implementation methods should only perform the task when called by interface
methods.
Example –
Thread safe single ton class Implementation with Double-Checked Locking technique.
Thread-Safe Interface
Implementation –
# To protect the critical section from concurrent access , apply Scoped Locking.
class Singleton {
public:
static Singleton *instance () {
if (instance_ == 0)
Guard<Thread_Mutex> guard (singleton_lock_);
instance_ = new Singleton;
return instance_;
} // Destructor releases lock automatically
private:
static Singleton *instance_;
static Thread_Mutex singleton_lock_;
Singleton() { };
Singleton(const Singleton &) { };
Singleton& operator= (const Singleton &) { };
};
Thread-Safe Interface
# Introduce a check to avoid modifying “ instance_ “ when multiple threads access it
class Singleton
public:
static Singleton *instance () {
if (instance_ == 0) {
Guard<Thread_Mutex> guard (singleton_lock_);
if (instance_ == 0) {
instance_ = new Singleton;
} }
return instance_;
} // Destructor releases lock automatically
private:
static Singleton *instance_;
static Thread_Mutex singleton_lock_;
Singleton() { };
Singleton(const Singleton &) { };
Singleton& operator= (const Singleton &) { };
};
Thank You

Your suggestions and comments are always welcome.
Please send me your feedback at
a_s_sinha@yahoo.com

More Related Content

PPTX
Architectural patterns part 4
PPT
Active Object
PDF
Active Object Design Pattern
PPTX
Architectural Patterns - Interactive and Event Handling Patterns
PPTX
C# Thread synchronization
PPT
Threads And Synchronization in C#
PPTX
Concurrency Programming in Java - 06 - Thread Synchronization, Liveness, Guar...
PDF
Context-Oriented Programming: Beyond Layers
Architectural patterns part 4
Active Object
Active Object Design Pattern
Architectural Patterns - Interactive and Event Handling Patterns
C# Thread synchronization
Threads And Synchronization in C#
Concurrency Programming in Java - 06 - Thread Synchronization, Liveness, Guar...
Context-Oriented Programming: Beyond Layers

What's hot (20)

PPTX
Types of MessageRouting in Mule
PPTX
Enterprise Design Pattern: ACID principal ,Concurrency Patterns
PPTX
Chain of Responsibility Pattern
PPT
Iterator Design Pattern
PPT
Tech talk
PDF
Java Tutorials - Concurrency
PPTX
Concurrency
PPT
Threads and Synchronization in c#
PPT
Lesson11 more behavioural patterns
DOCX
Process synchronization
PPTX
Multi-Threading
PPT
Command Design Pattern
PPTX
SCWCD : The servlet container : CHAP : 4
PPTX
Mule expression
PPTX
Concurrency
PDF
Design patterns - Singleton&Command
PDF
Android development training programme , Day 3
PPTX
React hooks
PPTX
M expression
PPTX
Understanding concurrency
Types of MessageRouting in Mule
Enterprise Design Pattern: ACID principal ,Concurrency Patterns
Chain of Responsibility Pattern
Iterator Design Pattern
Tech talk
Java Tutorials - Concurrency
Concurrency
Threads and Synchronization in c#
Lesson11 more behavioural patterns
Process synchronization
Multi-Threading
Command Design Pattern
SCWCD : The servlet container : CHAP : 4
Mule expression
Concurrency
Design patterns - Singleton&Command
Android development training programme , Day 3
React hooks
M expression
Understanding concurrency
Ad

Viewers also liked (6)

PPT
Pattern-Oriented Distributed Software Architectures
PDF
Tutorial on J2EE versus .NET for .NET Programmers
PPT
Pattern-Oriented Software Architecture: Patterns for Concurrent and Networked...
PPTX
Software Architecture Patterns
PPTX
Architectural styles and patterns
PPT
Architectural Patterns and Software Architectures: Client-Server, Multi-Tier,...
Pattern-Oriented Distributed Software Architectures
Tutorial on J2EE versus .NET for .NET Programmers
Pattern-Oriented Software Architecture: Patterns for Concurrent and Networked...
Software Architecture Patterns
Architectural styles and patterns
Architectural Patterns and Software Architectures: Client-Server, Multi-Tier,...
Ad

Similar to Architectural patterns part 3 (20)

PPTX
.NET: Thread Synchronization Constructs
DOC
Concurrency Learning From Jdk Source
PDF
Introduction+To+Java+Concurrency
PPTX
Threads and synchronization in C# visual programming.pptx
DOCX
systemverilog-interview-questions.docx
DOCX
Java 5 concurrency
PPTX
Java- Concurrent programming - Synchronization (part 1)
PPT
Java Multithreading and Concurrency
PPT
04 threads
PDF
Core Java Programming Language (JSE) : Chapter XII - Threads
PPT
The Pillars Of Concurrency
PDF
Spin Locks and Contention : The Art of Multiprocessor Programming : Notes
PDF
Android Http Connection and SAX Parsing
PPT
Multithreading
PDF
Struts2 tutorial
PDF
Struts2 tutorial
PDF
Building resilient applications
PPT
Java util concurrent
PPT
Java programming concept
PDF
Adv java unit 1 M.Sc CS.pdf
.NET: Thread Synchronization Constructs
Concurrency Learning From Jdk Source
Introduction+To+Java+Concurrency
Threads and synchronization in C# visual programming.pptx
systemverilog-interview-questions.docx
Java 5 concurrency
Java- Concurrent programming - Synchronization (part 1)
Java Multithreading and Concurrency
04 threads
Core Java Programming Language (JSE) : Chapter XII - Threads
The Pillars Of Concurrency
Spin Locks and Contention : The Art of Multiprocessor Programming : Notes
Android Http Connection and SAX Parsing
Multithreading
Struts2 tutorial
Struts2 tutorial
Building resilient applications
Java util concurrent
Java programming concept
Adv java unit 1 M.Sc CS.pdf

More from assinha (8)

PPTX
Nwe Embodiment (Naba Kalebara) of Lord Jagannath of PURI - The Greatest and B...
PPTX
SNMP AT a GLANCE
PPTX
Layer3protocols
PPTX
Umts explained
PPTX
Architectural patterns part 1
PPTX
Data Structures used in Linux kernel
PPTX
E nodeb handover procedure
PPT
Initial LTE call Setup Flow
Nwe Embodiment (Naba Kalebara) of Lord Jagannath of PURI - The Greatest and B...
SNMP AT a GLANCE
Layer3protocols
Umts explained
Architectural patterns part 1
Data Structures used in Linux kernel
E nodeb handover procedure
Initial LTE call Setup Flow

Recently uploaded (20)

PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
master seminar digital applications in india
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
Cell Structure & Organelles in detailed.
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
GDM (1) (1).pptx small presentation for students
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
Computing-Curriculum for Schools in Ghana
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Lesson notes of climatology university.
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPH.pptx obstetrics and gynecology in nursing
Module 4: Burden of Disease Tutorial Slides S2 2025
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
master seminar digital applications in india
human mycosis Human fungal infections are called human mycosis..pptx
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Cell Structure & Organelles in detailed.
VCE English Exam - Section C Student Revision Booklet
Final Presentation General Medicine 03-08-2024.pptx
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
GDM (1) (1).pptx small presentation for students
STATICS OF THE RIGID BODIES Hibbelers.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Complications of Minimal Access Surgery at WLH
Computing-Curriculum for Schools in Ghana
2.FourierTransform-ShortQuestionswithAnswers.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Lesson notes of climatology university.
Microbial diseases, their pathogenesis and prophylaxis

Architectural patterns part 3

  • 1. Architectural Patterns [PART 3] (Synchronization idioms & Pattern) based on Pattern-Oriented Software Architecture, Patterns for Concurrent and Networked Objects, Volume 2 by Douglas Schmidt, Michael Stal, Hans Rohnert and Frank Buschmann
  • 2. Scoped Locking It ensures that a lock is acquired automatically when control enters a scope and released automatically when control leaves the scope. Implementation 1. Define a guard class that acquires and releases a lock in its constructor and destructor respectively. class Thread_Mutex_Guard { private: Thread_Mutex *lock_; // Pointer to lock. bool owner_; // Set to true when a lock is acquired Thread_Mutex_Guard (const Thread_Mutex_Guard&); // //disallow copy constructor and = operator void operator= (const Thread_Mutex_Guard &); public: Thread_Mutex_Guard (Thread_Mutex &lock): lock_ (&lock), owner_ (false) { lock_->acquire (); owner_ = true; } ~Thread_Mutex_Guard () { if (owner_) lock_->release (); } };
  • 3. Scoped Locking 2. call the thread_Mutex_Guard class object inside the class / function Class test { int test1(int index) { Thread_Mutex_Guard guard (lock_); if (/* condition */) { // Do some more work ... return true; } // End of scope releases the lock. Return false ; // End of scope releases the lock. } Private : Thread_Mutex lock_; }; Mutex Lock is acquired and released automatically as control enters and leaves the test1() method respectively. Two specific functions acquire() and release() can be created inside the Thread_Mutex_Guard class to acquire the lock and release the lock. In acquire(), it should be verified if lock is already acquired. In release() should be verified if lock is already released. This way if lock is already released , then release() in destructor can verify it.
  • 4. Strategized Locking The Strategized Locking technique parameterizes synchronization mechanisms that protect a component's critical sections from concurrent access. To increase performance on large-scale multi-processor platforms, it may be required to change the synchronization strategy to more efficient e.g. from thread mutex to readers/writer lock which is time-consuming and error prone. Strategized Locking, the family of locking mechanisms becomes more reusable and easier to apply across applications. Implementation 1. Define an abstract interface for the locking mechanisms 2. Define a guard class and pass specific concrete lock object based on requirement. 3. Update the component interface and implementation
  • 5. Strategized Locking # Define an abstract interface for the locking mechanisms class Lock { public: // define methods. virtual void acquire () = 0; virtual void release () = 0; }; # Define concrete classes for each kind of lock (e.g. concrete class for mutex lock) class Thread_Mutex_Lock : public Lock { public: Virtual void acquire () { lock_.acquire (); } Virtual void release () { lock_.release (); } private: Thread_Mutex mutexLock_; // Concrete lock type. friend class Guard; // Define <test> as a friend so it can access <mutexLock_> };
  • 6. Strategized Locking # Define guard class class Guard { public: Guard (Thread_Mutex &lock): lock_ (&lock), owner_ (false) { lock_->acquire (); owner_ = true; } ~Guard () { if (owner_) lock_->release (); } private: // Pointer to the lock. Thread_Mutex *lock_; bool owner_; };
  • 7. Strategized Locking # Implement the interface Polymorphic lock can be passed to the component either as a parameter in its constructor or by adding a lock template parameter to the component declaration. class test 1{ public: // pass the Constructor the concrete lock object test (Lock & mutexLock_) : lock_ (mutexLock_) {}; // lock passed as a parameter in its constructor void Function1(const char *pathname) { Guard guard (lock_); //Critical section return; } private: Thread_Mutex *lock_; };
  • 8. Thread-Safe Interface This pattern ensures If a method that uses the Scoped Locking idiom, does not call itself recursively to avoid self-deadlock. Double-Checked Locking Optimization This technique avoids race conditions when accessing and modifying shared resources by concurrent application during program execution Implementation – • All interface methods, (e.g. C++ public methods) should only acquire/release component lock(s). • Implementation methods should only perform the task when called by interface methods. Example – Thread safe single ton class Implementation with Double-Checked Locking technique.
  • 9. Thread-Safe Interface Implementation – # To protect the critical section from concurrent access , apply Scoped Locking. class Singleton { public: static Singleton *instance () { if (instance_ == 0) Guard<Thread_Mutex> guard (singleton_lock_); instance_ = new Singleton; return instance_; } // Destructor releases lock automatically private: static Singleton *instance_; static Thread_Mutex singleton_lock_; Singleton() { }; Singleton(const Singleton &) { }; Singleton& operator= (const Singleton &) { }; };
  • 10. Thread-Safe Interface # Introduce a check to avoid modifying “ instance_ “ when multiple threads access it class Singleton public: static Singleton *instance () { if (instance_ == 0) { Guard<Thread_Mutex> guard (singleton_lock_); if (instance_ == 0) { instance_ = new Singleton; } } return instance_; } // Destructor releases lock automatically private: static Singleton *instance_; static Thread_Mutex singleton_lock_; Singleton() { }; Singleton(const Singleton &) { }; Singleton& operator= (const Singleton &) { }; };
  • 11. Thank You Your suggestions and comments are always welcome. Please send me your feedback at a_s_sinha@yahoo.com