Click here to Skip to main content
13,005,973 members (106,002 online)
Click here to Skip to main content
Add your own
alternative version


20 bookmarked
Posted 9 Feb 2009

Strategized Locking Pattern

, 9 Feb 2009
Rate this:
Please Sign up or sign in to vote.
The Strategized Locking design pattern parameterized synchronization mechanisms protect a component’s critical section from concurrent access.


The Strategized Locking design pattern parameterized synchronization mechanisms protect a component’s critical section from concurrent access. In other words, when an object requires data which is created within a different thread, it is necessary to lock the critical sections for data access. Just imagine the result of a data when an object within a thread is writing the shared data, and simultaneously a different object is reading this data from a different thread. You never know the result…

A critical section can be locked for example by creating and using a Mutex. There are different ways to create, take and release a Mutex. However, it is of utmost importance to follow these three steps carefully to prevent lock failures and/or deadlocks. This pattern holds the responsibility for these three actions. Moreover, it wraps the Windows Mutex methods within a dedicated object.

Detailed Information

Detailed information can be found in this book: Pattern-Oriented Software Architecture (Volume 2).

When to Use this Pattern?

Use this pattern if:

  • You want to guard a critical section
  • You want to be consistent with using mutex
  • You want a more readable code
  • You don’t want to implement a mutex over and over again
  • You don’t want to create a deadlock

Using the Strategized Locking Pattern

This pattern exists of three parts:

  • Part 1: Wrapper around the Windows Mutex methods
  • Part 2: The responsibility for creating, taking and releasing the Mutex
  • Part 3: The interface for objects which are lockable

UML Class Diagram

The Sensor class is used as an example.  

  • Lock: The wrapper for the Windows Mutex calls
  • Guard: Responsible for taking and releasing the Mutex
  • ILockable: Responsible for providing the interface for locking shared data within lockable objects

Sequence Diagram

There are 3 ways to use this pattern:

Usage 1


Usage 2


Usage 3


How to Use the Code

Usage 1

Create a Mutex, take it, and release it.
The header file *.h:

       Lock m_lock;
       unsigned int m_count;

The implementation file *.cpp:

m_lock.Take( );
m_count++; // critical section
m_lock.Release( ); // make sure you release it! 

Usage 2 (Polymorphism)

In this approach, pass a polymorphic lock object to the guard's constructor and define an instance of this lock object as a private data member.
The header file *.h:

    Lock m_lock;
    unsigned int m_count;

The implementation file *.cpp:

Guard guard( m_lock );	// no release is needed
m_count++; // critical section

Usage 3 (Parameterized)

In this approach, define a template guard class that is parameterized by the type of lock that will be acquired and released automatically. Inherit your object from the ILockable interface.

class Sensor : public ILockable
    // constructor
    Sensor( );

Implement the ILockable methods:

    * @see ILockable::Take().
    * @since    <30 01 2009 >
    virtual void Take( );

    * ILockable::Release().
    * @since    <30 01 2009 >
    virtual void Release( );

I have selected to lock the Sensor object as an example.

The implementation file *.cpp:

Guarder< ILockable > guarder( *this ) ;

or the header file *.h:

    Lock m_lock;

The implementation file *.cpp:

Guarder< Lock > guarder( m_lock ) ;


  • 9th February, 2009: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Hamed Ebrahimmalek
Software Developer
Netherlands Netherlands
I'm a C++ and C# .Net software engineer.

You may also be interested in...

Comments and Discussions

GeneralGood article Pin
Donsw4-Mar-09 10:50
memberDonsw4-Mar-09 10:50 
I agtree the diagrams need work but overall good.

My Recent Article : Optimistic Concurrency with C# using the IOC and DI Design Patterns

GeneralThe class diagram is very confusing Pin
Zhou Feng18-Feb-09 14:48
memberZhou Feng18-Feb-09 14:48 
GeneralRe: The class diagram is very confusing Pin
Hamed Ebrahimmalek18-Feb-09 19:48
memberHamed Ebrahimmalek18-Feb-09 19:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170628.1 | Last Updated 9 Feb 2009
Article Copyright 2009 by Hamed Ebrahimmalek
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid