Qore Programming Language Reference Manual 1.18.0
|
A helper class for the Mutex class for exception-safe Mutex handling. More...
#include <QC_AutoLock.dox.h>
Public Member Methods | |
constructor (Mutex mutex) | |
Creates the AutoLock object based on the Mutex argument passed and immediately calls Mutex::lock() More... | |
copy () | |
Throws an exception; objects of this class cannot be copied. More... | |
destructor () | |
Calls Mutex::unlock() on the saved Mutex object and destroys the AutoLock object. More... | |
nothing | lock () |
Attempts to relock the Mutex object being managed. More... | |
int | trylock () |
Attempts to relock the Mutex object being managed; acquires the lock only if it is not already held; returns 0 for success (lock acquired) or -1 if the call would block. More... | |
nothing | unlock () |
Unlocks the Mutex object being managed; wakes up one thread if any threads are blocked on this lock. More... | |
A helper class for the Mutex class for exception-safe Mutex handling.
AutoLock objects, when used along with a Mutex object, allow Qore programmers to safely acquire and release a Mutex lock, even if exceptions are thrown or return statements are executed in the block where the AutoLock object is created.
AutoLock objects are helper objects that acquire a Mutex for the lifetime of the object.
For this reason, it is only appropriate to assign an AutoLock object to a local variable, so when the local variable goes out of scope, the AutoLock object will be deleted and the Mutex will be automatically released.
For example:
The destructor will call Mutex::unlock() only if the current thread owns the lock, so it is safe to unlock the lock manually (or by calling AutoLock::unlock()) while the AutoLock object is in scope.
Qore::Thread::AutoLock::constructor | ( | Mutex | mutex | ) |
Creates the AutoLock object based on the Mutex argument passed and immediately calls Mutex::lock()
The AutoLock object immediately calls Mutex::lock() on the Mutex object passed, and saves it; Mutex::unlock() is called in the destructor if the lock is still held by the current thread.
mutex | a Mutex object to lock immediately and hold for the scope of the AutoLock object (unless manually unlocked) |
LOCK-ERROR | lock called twice in the same thread, Mutex object has already been deleted in another thread, etc |
THREAD-DEADLOCK | a deadlock was detected while trying to acquire the lock |
Qore::Thread::AutoLock::copy | ( | ) |
Throws an exception; objects of this class cannot be copied.
AUTOLOCK-COPY-ERROR | Objects of this class cannot be copied |
Qore::Thread::AutoLock::destructor | ( | ) |
Calls Mutex::unlock() on the saved Mutex object and destroys the AutoLock object.
Mutex::unlock() is only called if the current thread owns the lock
nothing Qore::Thread::AutoLock::lock | ( | ) |
Attempts to relock the Mutex object being managed.
Do not call this method unless the Mutex object being managed has been unlocked since the constructor
LOCK-ERROR | lock called twice in the same thread, Mutex object has already been deleted in another thread, etc |
THREAD-DEADLOCK | a deadlock was detected while trying to acquire the lock |
int Qore::Thread::AutoLock::trylock | ( | ) |
Attempts to relock the Mutex object being managed; acquires the lock only if it is not already held; returns 0 for success (lock acquired) or -1 if the call would block.
LOCK-ERROR | object deleted in another thread, etc |
THREAD-DEADLOCK | a deadlock was detected while trying to acquire the lock |
nothing Qore::Thread::AutoLock::unlock | ( | ) |
Unlocks the Mutex object being managed; wakes up one thread if any threads are blocked on this lock.
LOCK-ERROR | unlock called by a thread that does not own the lock or the lock is not locked, object deleted in another thread, etc |