An object that can be locked by one thread to exclude other threads until it is unlocked.
class CXlLockable |
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.
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.
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 }
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 |
Header: xlpshared.h
CXlLockable Methods | xlpshared.h | Ensuring global data is thread-safe | CXlLock