Qore Programming Language Reference Manual: Qore::Thread::AutoLock Class Reference (original) (raw)
A helper class for the Mutex class for exception-safe Mutex handling.
Restrictions:
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:
our Mutex mutex();
sub check_error(error) {
AutoLock al(mutex);
if (error)
throw "ERROR", "sorry, an error happened";
return "OK";
}
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.
Note
This class is not available with the PO_NO_THREAD_CLASSES parse option.