Click here to Skip to main content
11,803,019 members (81,021 online)
Click here to Skip to main content

Shared and Exclusive lock control

, 31 Aug 2004 35.7K 585 11
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 
Sub par coding quality. Slow.

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151002.1 | Last Updated 1 Sep 2004
Article Copyright 2004 by Charles Bargar
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid