PCL
pcl::AutoLockCounter Class Reference

Automatic mutex lock/unlock with limited concurrent access allowance. More...

#include <AutoLock.h>

Public Member Functions

 AutoLockCounter (AutoLockCounter &&x)
 
 AutoLockCounter (const AutoLockCounter &)=delete
 
 AutoLockCounter (pcl::Mutex &mutex, AtomicInt &count, int limit)
 
 ~AutoLockCounter ()
 
void Lock ()
 
AutoLockCounteroperator= (AutoLockCounter &&)=delete
 
AutoLockCounteroperator= (const AutoLockCounter &)=delete
 
void Unlock ()
 

Detailed Description

AutoLockCounter is similar to AutoLock: it allows protecting a section of code against concurrent thread access through a Mutex, with automatic lock/unlock operations upon object construction/destruction. However, AutoLockCounter is slightly more complex because it can allow a specified amount of concurrent accesses, while AutoLock forbids them completely.

AutoLockCounter is useful for scenarios where the protected code can exploit a resource concurrently, but only to a given extent that can be predicted or configured. Typical examples are routines performing file read/write operations.

Example:

void ParallelWriteFile( const String& filePath, const ByteArray& data, int limit )
{
static Mutex mutex;
static AtomicInt count;
volatile AutoLockCounter lock( mutex, count, limit );
File::WriteFile( filePath, data );
}

In this example, the ParallelWriteFile routine allows creating and writing up to limit different disk files simultaneously. If limit files are already being written concurrently, a new call to ParallelWriteFile() will block the caller thread until at least one of the running tasks terminates.

See also
AutoLock, Mutex, AtomicInt

Definition at line 207 of file AutoLock.h.

Constructor & Destructor Documentation

◆ AutoLockCounter() [1/3]

pcl::AutoLockCounter::AutoLockCounter ( pcl::Mutex mutex,
AtomicInt count,
int  limit 
)
inlineexplicit

Constructs an AutoLockCounter object to monitor a Mutex with automatic counting of lock operations and a given maximum number of concurrent accesses.

Parameters
mutexReference to a Mutex object that will be monitored by this AutoLockCounter instance.
countReference to an AtomicInt object that will be updated by this instance to control the current amount of concurrent accesses.
limitMaximum number of concurrent accesses allowed. The specified count variable will be atomically incremented by this constructor. If count is greater than limit after the increment, this constructor will lock the specified mutex. Otherwise the mutex will not be locked automatically upon construction. Note that the mutex can be locked explicitly by calling the Lock() member function for any AutoLock or AutoLockCounter object sharing the same mutex variable.

By specifying a limit of zero, no concurrent access will be allowed and this object will be functionally equivalent to an AutoLock instance monitoring the same mutex. By setting limit to a value greater than or equal to one, the protected code section will be allowed to run once, twice, etc. without a (potentially expensive) lock operation.

If the specified mutex has been locked by this object, be it automatically or explicitly, it will be unlocked automatically when this AutoLockCounter object is destroyed or gets out of scope.

Definition at line 243 of file AutoLock.h.

◆ AutoLockCounter() [2/3]

pcl::AutoLockCounter::AutoLockCounter ( AutoLockCounter &&  x)
inline

Move constructor.

Definition at line 259 of file AutoLock.h.

◆ ~AutoLockCounter()

pcl::AutoLockCounter::~AutoLockCounter ( )
inline

Destroys this AutoLockCounter object.

This destructor performs two separate actions:

  • If the monitored mutex (which was specified in the constructor) has been locked by this object, it will be unlocked. This applies both if the mutex has been locked automatically upon construction (because the monitored counter was larger than the specified limit), or explicitly by calling the Lock() member function for this object.
  • The monitored counter, which was atomically incremented upon construction, will be atomically decremented.

Definition at line 282 of file AutoLock.h.

◆ AutoLockCounter() [3/3]

pcl::AutoLockCounter::AutoLockCounter ( const AutoLockCounter )
delete

Copy constructor. This constructor is disabled because AutoLockCounter objects cannot be copied.

Member Function Documentation

◆ Lock()

void pcl::AutoLockCounter::Lock ( )
inline

Locks the monitored mutex object, if it has not been previously locked by this object.

Definition at line 315 of file AutoLock.h.

◆ operator=() [1/2]

AutoLockCounter& pcl::AutoLockCounter::operator= ( AutoLockCounter &&  )
delete

Move assignment. This operator is disabled because AutoLockCounter objects cannot be move-assigned.

◆ operator=() [2/2]

AutoLockCounter& pcl::AutoLockCounter::operator= ( const AutoLockCounter )
delete

Copy assignment. This operator is disabled because AutoLockCounter objects cannot be copied.

◆ Unlock()

void pcl::AutoLockCounter::Unlock ( )
inline

Unlocks the monitored mutex object, if it has been previously locked by this object.

Definition at line 326 of file AutoLock.h.


The documentation for this class was generated from the following file:
pcl::AutoLockCounter::AutoLockCounter
AutoLockCounter(pcl::Mutex &mutex, AtomicInt &count, int limit)
Definition: AutoLock.h:243
pcl::File::WriteFile
static void WriteFile(const String &filePath, const ByteArray &contents)
ByteArray
Dynamic array of 8-bit unsigned integers.