ResourcesLocker Class Reference

The class to use to acquire the lock on resources. More...

Public Member Functions

 ResourcesLocker (ConcurrentResourcesUser *resourcesUser, bool acquireLock=true)
 Constructor.
 
 ~ResourcesLocker ()
 Destructor.
 
void lock ()
 Acquires the lock.
 
void unlock ()
 Releases the lock.
 

Detailed Description

The class to use to acquire the lock on resources.

The ConcurrentResourcesUser class has no public methods to acquire the lock on the resources it uses. To acquire the lock use a local instance of ResourcesLocker so that you are also sure that the lock is released when the object is destroyed. Here is an example (suppose the current instance of A inherits from ConcurrentResourcesUser):

void A::f()
{
ResourcesLocker locker(this);
... use resources ...
}

This class also has methods to manually lock and unlock resources, although their use is discouraged. Multiple calls to lock() will always result in the lock being acquired once (even if the underlying semaphores are recursive), so a single call to unlock() is enough to unlock the resource no matter how many times you called lock(). Similarly, only the first one of subsequent calls to unlock() actually unlocks. If however you create multiple ResourcesLocker objects on the same ConcurrentResourcesUser object in the same thread, you must unlock all ResourcesLocker to actually unlock resources (see the warning below)

Warning
You can set two or more ResourcesLocker on the same ConcurrentResourcesUser object in the same thread, locking will be recursive (i.e. you will have to unlock the same number of times you have locked to actually unlock)

Definition at line 1098 of file resourcesuser.h.

Constructor & Destructor Documentation

ResourcesLocker ( ConcurrentResourcesUser resourcesUser,
bool  acquireLock = true 
)

Constructor.

Parameters
resourcesUserthe resource user with the resources to lock
acquireLockif true the lock is acquired automatically (and this function will block), if false it is not. It is advised to let the constructor acquire the lock and to let the destructor release them.

Definition at line 702 of file resourcesuser.cpp.

References ResourcesLocker::lock().

Destructor.

This releases the lock if it is still held

Definition at line 711 of file resourcesuser.cpp.

References ResourcesLocker::unlock().

Member Function Documentation

void lock ( )

Acquires the lock.

This function blocks until the lock is acquired

Definition at line 716 of file resourcesuser.cpp.

Referenced by ResourcesLocker::ResourcesLocker().

void unlock ( )

Releases the lock.

We don't check here if the mutex was locked or not, be careful!

Definition at line 727 of file resourcesuser.cpp.

Referenced by ResourcesLocker::~ResourcesLocker().


The documentation for this class was generated from the following files: