SlideShare una empresa de Scribd logo
1 de 11
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

Más contenido relacionado

La actualidad más candente

Enterprise Design Pattern: ACID principal ,Concurrency Patterns
Enterprise Design Pattern:ACID principal,Concurrency PatternsEnterprise Design Pattern:ACID principal,Concurrency Patterns
Enterprise Design Pattern: ACID principal ,Concurrency PatternsSergey Bandysik
 
Chain of Responsibility Pattern
Chain of Responsibility PatternChain of Responsibility Pattern
Chain of Responsibility PatternHüseyin Ergin
 
Iterator Design Pattern
Iterator Design PatternIterator Design Pattern
Iterator Design PatternVarun Arora
 
Threads and Synchronization in c#
Threads and Synchronization in c#Threads and Synchronization in c#
Threads and Synchronization in c#Rizwan Ali
 
Lesson11 more behavioural patterns
Lesson11 more behavioural patternsLesson11 more behavioural patterns
Lesson11 more behavioural patternsOktJona
 
Process synchronization
Process synchronizationProcess synchronization
Process synchronizationlodhran-hayat
 
Command Design Pattern
Command Design PatternCommand Design Pattern
Command Design PatternShahriar Hyder
 
SCWCD : The servlet container : CHAP : 4
SCWCD : The servlet container : CHAP : 4SCWCD : The servlet container : CHAP : 4
SCWCD : The servlet container : CHAP : 4Ben Abdallah Helmi
 
Design patterns - Singleton&Command
Design patterns - Singleton&CommandDesign patterns - Singleton&Command
Design patterns - Singleton&CommandKai Aras
 
Android development training programme , Day 3
Android development training programme , Day 3Android development training programme , Day 3
Android development training programme , Day 3DHIRAJ PRAVIN
 
Understanding concurrency
Understanding concurrencyUnderstanding concurrency
Understanding concurrencyAnshul Sharma
 

La actualidad más candente (20)

Types of MessageRouting in Mule
Types of MessageRouting in MuleTypes of MessageRouting in Mule
Types of MessageRouting in Mule
 
Enterprise Design Pattern: ACID principal ,Concurrency Patterns
Enterprise Design Pattern:ACID principal,Concurrency PatternsEnterprise Design Pattern:ACID principal,Concurrency Patterns
Enterprise Design Pattern: ACID principal ,Concurrency Patterns
 
Chain of Responsibility Pattern
Chain of Responsibility PatternChain of Responsibility Pattern
Chain of Responsibility Pattern
 
Iterator Design Pattern
Iterator Design PatternIterator Design Pattern
Iterator Design Pattern
 
Tech talk
Tech talkTech talk
Tech talk
 
Java Tutorials - Concurrency
Java Tutorials - ConcurrencyJava Tutorials - Concurrency
Java Tutorials - Concurrency
 
Concurrency
ConcurrencyConcurrency
Concurrency
 
Threads and Synchronization in c#
Threads and Synchronization in c#Threads and Synchronization in c#
Threads and Synchronization in c#
 
Lesson11 more behavioural patterns
Lesson11 more behavioural patternsLesson11 more behavioural patterns
Lesson11 more behavioural patterns
 
Process synchronization
Process synchronizationProcess synchronization
Process synchronization
 
Multi-Threading
Multi-ThreadingMulti-Threading
Multi-Threading
 
Command Design Pattern
Command Design PatternCommand Design Pattern
Command Design Pattern
 
SCWCD : The servlet container : CHAP : 4
SCWCD : The servlet container : CHAP : 4SCWCD : The servlet container : CHAP : 4
SCWCD : The servlet container : CHAP : 4
 
Mule expression
Mule expressionMule expression
Mule expression
 
Concurrency
ConcurrencyConcurrency
Concurrency
 
Design patterns - Singleton&Command
Design patterns - Singleton&CommandDesign patterns - Singleton&Command
Design patterns - Singleton&Command
 
Android development training programme , Day 3
Android development training programme , Day 3Android development training programme , Day 3
Android development training programme , Day 3
 
React hooks
React hooksReact hooks
React hooks
 
M expression
M expressionM expression
M expression
 
Understanding concurrency
Understanding concurrencyUnderstanding concurrency
Understanding concurrency
 

Destacado

Pattern-Oriented Distributed Software Architectures
Pattern-Oriented Distributed Software Architectures Pattern-Oriented Distributed Software Architectures
Pattern-Oriented Distributed Software Architectures David Freitas
 
Tutorial on J2EE versus .NET for .NET Programmers
Tutorial on J2EE versus .NET for .NET Programmers Tutorial on J2EE versus .NET for .NET Programmers
Tutorial on J2EE versus .NET for .NET Programmers David Freitas
 
Pattern-Oriented Software Architecture: Patterns for Concurrent and Networked...
Pattern-Oriented Software Architecture: Patterns for Concurrent and Networked...Pattern-Oriented Software Architecture: Patterns for Concurrent and Networked...
Pattern-Oriented Software Architecture: Patterns for Concurrent and Networked...David Freitas
 
Software Architecture Patterns
Software Architecture PatternsSoftware Architecture Patterns
Software Architecture PatternsAssaf Gannon
 
Architectural styles and patterns
Architectural styles and patternsArchitectural styles and patterns
Architectural styles and patternsHimanshu
 
Architectural Patterns and Software Architectures: Client-Server, Multi-Tier,...
Architectural Patterns and Software Architectures: Client-Server, Multi-Tier,...Architectural Patterns and Software Architectures: Client-Server, Multi-Tier,...
Architectural Patterns and Software Architectures: Client-Server, Multi-Tier,...Svetlin Nakov
 

Destacado (6)

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

Similar a Architectural patterns part 3

.NET: Thread Synchronization Constructs
.NET: Thread Synchronization Constructs.NET: Thread Synchronization Constructs
.NET: Thread Synchronization ConstructsSasha Kravchuk
 
Concurrency Learning From Jdk Source
Concurrency Learning From Jdk SourceConcurrency Learning From Jdk Source
Concurrency Learning From Jdk SourceKaniska Mandal
 
systemverilog-interview-questions.docx
systemverilog-interview-questions.docxsystemverilog-interview-questions.docx
systemverilog-interview-questions.docxssuser1c8ca21
 
Java 5 concurrency
Java 5 concurrencyJava 5 concurrency
Java 5 concurrencypriyank09
 
Java- Concurrent programming - Synchronization (part 1)
Java- Concurrent programming - Synchronization (part 1)Java- Concurrent programming - Synchronization (part 1)
Java- Concurrent programming - Synchronization (part 1)Riccardo Cardin
 
Java Multithreading and Concurrency
Java Multithreading and ConcurrencyJava Multithreading and Concurrency
Java Multithreading and ConcurrencyRajesh Ananda Kumar
 
Core Java Programming Language (JSE) : Chapter XII - Threads
Core Java Programming Language (JSE) : Chapter XII -  ThreadsCore Java Programming Language (JSE) : Chapter XII -  Threads
Core Java Programming Language (JSE) : Chapter XII - ThreadsWebStackAcademy
 
The Pillars Of Concurrency
The Pillars Of ConcurrencyThe Pillars Of Concurrency
The Pillars Of Concurrencyaviade
 
Spin Locks and Contention : The Art of Multiprocessor Programming : Notes
Spin Locks and Contention : The Art of Multiprocessor Programming : NotesSpin Locks and Contention : The Art of Multiprocessor Programming : Notes
Spin Locks and Contention : The Art of Multiprocessor Programming : NotesSubhajit Sahu
 
Android Http Connection and SAX Parsing
Android Http Connection and SAX ParsingAndroid Http Connection and SAX Parsing
Android Http Connection and SAX ParsingJussi Pohjolainen
 
Multithreading
MultithreadingMultithreading
Multithreadingbackdoor
 
Building resilient applications
Building resilient applicationsBuilding resilient applications
Building resilient applicationsNuno Caneco
 
Java util concurrent
Java util concurrentJava util concurrent
Java util concurrentRoger Xia
 
Java programming concept
Java programming conceptJava programming concept
Java programming conceptSanjay Gunjal
 
Adv java unit 1 M.Sc CS.pdf
Adv java unit 1 M.Sc CS.pdfAdv java unit 1 M.Sc CS.pdf
Adv java unit 1 M.Sc CS.pdfKALAISELVI P
 
Multithreading Presentation
Multithreading PresentationMultithreading Presentation
Multithreading PresentationNeeraj Kaushik
 

Similar a Architectural patterns part 3 (20)

.NET: Thread Synchronization Constructs
.NET: Thread Synchronization Constructs.NET: Thread Synchronization Constructs
.NET: Thread Synchronization Constructs
 
Concurrency Learning From Jdk Source
Concurrency Learning From Jdk SourceConcurrency Learning From Jdk Source
Concurrency Learning From Jdk Source
 
Introduction+To+Java+Concurrency
Introduction+To+Java+ConcurrencyIntroduction+To+Java+Concurrency
Introduction+To+Java+Concurrency
 
systemverilog-interview-questions.docx
systemverilog-interview-questions.docxsystemverilog-interview-questions.docx
systemverilog-interview-questions.docx
 
Java 5 concurrency
Java 5 concurrencyJava 5 concurrency
Java 5 concurrency
 
Java- Concurrent programming - Synchronization (part 1)
Java- Concurrent programming - Synchronization (part 1)Java- Concurrent programming - Synchronization (part 1)
Java- Concurrent programming - Synchronization (part 1)
 
Java Multithreading and Concurrency
Java Multithreading and ConcurrencyJava Multithreading and Concurrency
Java Multithreading and Concurrency
 
04 threads
04 threads04 threads
04 threads
 
Core Java Programming Language (JSE) : Chapter XII - Threads
Core Java Programming Language (JSE) : Chapter XII -  ThreadsCore Java Programming Language (JSE) : Chapter XII -  Threads
Core Java Programming Language (JSE) : Chapter XII - Threads
 
The Pillars Of Concurrency
The Pillars Of ConcurrencyThe Pillars Of Concurrency
The Pillars Of Concurrency
 
Spin Locks and Contention : The Art of Multiprocessor Programming : Notes
Spin Locks and Contention : The Art of Multiprocessor Programming : NotesSpin Locks and Contention : The Art of Multiprocessor Programming : Notes
Spin Locks and Contention : The Art of Multiprocessor Programming : Notes
 
Android Http Connection and SAX Parsing
Android Http Connection and SAX ParsingAndroid Http Connection and SAX Parsing
Android Http Connection and SAX Parsing
 
Multithreading
MultithreadingMultithreading
Multithreading
 
Struts2 tutorial
Struts2 tutorialStruts2 tutorial
Struts2 tutorial
 
Struts2 tutorial
Struts2 tutorialStruts2 tutorial
Struts2 tutorial
 
Building resilient applications
Building resilient applicationsBuilding resilient applications
Building resilient applications
 
Java util concurrent
Java util concurrentJava util concurrent
Java util concurrent
 
Java programming concept
Java programming conceptJava programming concept
Java programming concept
 
Adv java unit 1 M.Sc CS.pdf
Adv java unit 1 M.Sc CS.pdfAdv java unit 1 M.Sc CS.pdf
Adv java unit 1 M.Sc CS.pdf
 
Multithreading Presentation
Multithreading PresentationMultithreading Presentation
Multithreading Presentation
 

Más de assinha

Nwe Embodiment (Naba Kalebara) of Lord Jagannath of PURI - The Greatest and B...
Nwe Embodiment (Naba Kalebara) of Lord Jagannath of PURI - The Greatest and B...Nwe Embodiment (Naba Kalebara) of Lord Jagannath of PURI - The Greatest and B...
Nwe Embodiment (Naba Kalebara) of Lord Jagannath of PURI - The Greatest and B...assinha
 
SNMP AT a GLANCE
SNMP AT a GLANCESNMP AT a GLANCE
SNMP AT a GLANCEassinha
 
Layer3protocols
Layer3protocolsLayer3protocols
Layer3protocolsassinha
 
Umts explained
Umts explainedUmts explained
Umts explainedassinha
 
Architectural patterns part 1
Architectural patterns part 1Architectural patterns part 1
Architectural patterns part 1assinha
 
Data Structures used in Linux kernel
Data Structures used in Linux kernel Data Structures used in Linux kernel
Data Structures used in Linux kernel assinha
 
E nodeb handover procedure
E nodeb handover procedureE nodeb handover procedure
E nodeb handover procedureassinha
 
Initial LTE call Setup Flow
Initial LTE call Setup FlowInitial LTE call Setup Flow
Initial LTE call Setup Flowassinha
 

Más de assinha (8)

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

Último

Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxNikitaBankoti2
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 

Último (20)

Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 

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