PCL
|
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 () |
AutoLockCounter & | operator= (AutoLockCounter &&)=delete |
AutoLockCounter & | operator= (const AutoLockCounter &)=delete |
void | Unlock () |
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:
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.
Definition at line 207 of file AutoLock.h.
|
inlineexplicit |
Constructs an AutoLockCounter object to monitor a Mutex with automatic counting of lock operations and a given maximum number of concurrent accesses.
mutex | Reference to a Mutex object that will be monitored by this AutoLockCounter instance. |
count | Reference to an AtomicInt object that will be updated by this instance to control the current amount of concurrent accesses. |
limit | Maximum 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.
|
inline |
Move constructor.
Definition at line 259 of file AutoLock.h.
|
inline |
Destroys this AutoLockCounter object.
This destructor performs two separate actions:
Definition at line 282 of file AutoLock.h.
|
delete |
Copy constructor. This constructor is disabled because AutoLockCounter objects cannot be copied.
|
inline |
Locks the monitored mutex object, if it has not been previously locked by this object.
Definition at line 315 of file AutoLock.h.
|
delete |
Move assignment. This operator is disabled because AutoLockCounter objects cannot be move-assigned.
|
delete |
Copy assignment. This operator is disabled because AutoLockCounter objects cannot be copied.
|
inline |
Unlocks the monitored mutex object, if it has been previously locked by this object.
Definition at line 326 of file AutoLock.h.