Click here to Skip to main content
13,550,835 members
Click here to Skip to main content
Add your own
alternative version


11 bookmarked
Posted 25 Aug 2004

Shared and Exclusive lock control

, 31 Aug 2004
Rate this:
Please Sign up or sign in to vote.
C++ class for shared and exclusive lock control.


This article shows a C++ class that provides access to named locks on a Shared and Exclusive basis within a process. The function is similar to the IBM OS/390 ENQ/DEQ macros.

  1. RET=HAVE is implied.
  2. The lock name is LOCKNAME/LOCKNAME_LENGTH pair rather than a QNAME/RNAME/RNAMEL combination.

A thread can request exclusive or shared ownership of a lock. Ownership is granted based on the following table:


Held by other threads














Threads placed in a wait state are released when the lock becomes available. Any EXCLUSIVE request blocks all requests following it until the EXCLUSIVE request is released.


Written in ANSI C++, except for the three internal classes described below.

Needed to be able to refine the Windows CRITICAL_SECTION function into shareable and exclusive locks for thread control.

Can be ported to other environments with minimal changes.


  • Internal class EXCLUSIVE_EX.
  • Internal class Semaphore.
  • Internal class ThreadIdentity

Using the code

Lock names may contain binary codes, for instance, an address. All compares, copies, etc. use the mem*** C++ functions. The maximum length for a lock name is 64 bytes (parameterized within locker.h).

How to use Locker:

In the parent thread, create an instance of the Locker class.

Locker  *Lockset;
Lockset=new Locker()

Pass Lockset to child threads that need access to the locks.

In any thread with access to Lockset:

int rc;

rc=Lockset->GetSharedLock(const char *lname, int lnamel);
  Returns LOCK_OK        (lock established)
          LOCK_SHARED    (Shared lock already held)
          LOCK_EXCL      (Exclusive lock already held)

rc=Lockset->GetExclLock(const char *lname, int lnamel);
  Returns LOCK_OK     (lock established)
          LOCK_SHARED (Shared lock already held)
                      The lock state was NOT changed
                      to Exclusive, and remains in
                      effect as Shared.
          LOCK_EXCL   (Exclusive lock already held)

rc=Lockset->RelLock(const char *lname, int lnamel);
  Returns LOCK_OK          (lock released)
          LOCK_NOT_HELD    (lock wasn't held)

rc=Lockset->TestLock(const char *lname, int lnamel);
  Returns LOCK_SHARED   (Shared lock held)
          LOCK_EXCL     (Exclusive lock held)
          LOCK_NOT_HELD (lock wasn't held)

When all threads are no longer using the Lockset:

delete Lockset;

If any threads are still using the Lockset, the destructor terminates the process with an assert(0), preventing program checks in the active threads when they attempt to access the deleted class.


  • First release.
  • Version 1.1
    1. Fixed a bug that caused a loop during RelLock
    2. Performance enhancement and code simplification - changed RelLock to not remove the Lock structure when no users of a lock remain. Lock structures are now removed a destructor time only.
    3. Updated Locker.h Locker.cpp to move all Windows-specific code into Locker internal classes, making ports to other systems easier.
    4. Updated Tlocker.cpp to use the data base address as the lock.
    5. Added tlocker_readme.txt file to describe the test program and how to run it.
    6. Created a separate VC workspace that contains only the Locker project (Locker.dws, Locker.dsp, and Tlocker.dsp).


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Charles Bargar
Software Developer (Senior)
United States United States
Application and systems programmer since 1972.
Mostly mainframe programming - Assembler, PL/X.
Products: IBM: DFSMSHSM, Candle Corp: Omegamon (IMS and MVS), PKWARE: SecureZIP

Dabble in Windows and Linux at home - C++ and Intel Assembler.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 1 Pin
C0D1F1ED27-Jan-09 0:24
memberC0D1F1ED27-Jan-09 0:24 
GeneralC centric lock system Pin
Nathan Holt at CCEI7-Sep-04 5:41
memberNathan Holt at CCEI7-Sep-04 5:41 
Your lock system seems centered around C rather than C++ in two ways.

First, you require lock names to be passed as a char * and a length, instead of as string objects, and worse still, you have specified a maximum length for names, which would not be necesary if you were using a string class internally. You could maximize portability by using std::string or std::vector<char> for lock names.

Second, you have provided lock and unlock functions, but not lock classes. In C++, one usually acquires a lock in the following maner:

    SharedLock Lock( "Lock Name", LockSet );
    // Do something.

    // The shared lock's destructor
    // automatically unlocks.

This construct is especially important when exceptions might be thrown in the critical section, since the destructor will still be called even with out a try/catch statement.

Nathan Holt

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
Web01-2016 | 2.8.180515.1 | Last Updated 1 Sep 2004
Article Copyright 2004 by Charles Bargar
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid