XLL+ Class Library (6.3)

CXlLockable Class

An object that can be locked by one thread to exclude other threads until it is unlocked.

class CXlLockable

Overview

The CXlLockable class can be used to lock an object for thread-safe access.

The object methods Lock an Unlock are used to control access to the object. Once a thread has successfully locked the object, the same thread may apply further locks without restriction. When an equal number of unlocks are applied, the object becomes available for other threads to use.

If a thread calls Lock while the object is already locked by another thread, then the first thread will wait until the object is unlocked.

CXlLock

The most convenient way to lock the object is by using a CXlLock object. CXlLock calls CXlLockable::Lock() when it is constructed, and calls CXlLockable::Unlock() when it is destroyed.

A typical usage is shown below.

CopyC++
CXlLockable sharedObject;
void UseSharedObject()
{
    CXlLock lock(sharedObject, true);
    // Do something with the sharedObject 
    // On exit from this function, the CXlLock instance will 
    // be destroyed, and sharedObject will be unlocked
}

Implementation

The CXlLocakable class is implemented as a thin layer on top of the Windows CRITICALSECTION object. The API calls for each of the class methods are listed below.

Method API call
Constructor InitializeCriticalSection
Destructor DeleteCriticalSection
Lock EnterCriticalSection
Unlock LeaveCriticalSection

Requirements

Header: xlpshared.h

See Also

CXlLockable Methods | xlpshared.h | Ensuring global data is thread-safe | CXlLock