Note: This class is not available with the PO_NO_THREAD_CLASSES
parse option.
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 = new Mutex(); sub check_error($error) { # note that the Mutex is acquired in the AutoLock constructor, and # the Mutex will be released as soon as the block is exited below. # (with either the throw statement or the return statement) my $am = new AutoLock($mutex); if ($error) throw "ERROR", "sorry, an error happened"; return "OK"; }
Table 4.850. AutoLock Method Overview
Method | Except? | Description |
---|---|---|
Y | Creates the AutoLock object based on the Mutex argument passed and immediately calls Mutex::lock(). | |
Y | Calls Mutex::unlock() on the saved Mutex and destroys the AutoLock object. | |
Y | Throws an exception; objects of this class cannot be copied. |
Creates the AutoLock object based on the Mutex argument passed. The AutoLock object immediately calls Mutex::lock() on the Mutex object passed, and saves it so it can be released when the AutoLock object is destroyed.
new AutoLock(mutex_object
)
my $al = new AutoLock($mutex);
Table 4.851. Arguments for AutoLock::constructor()
Argument | Type | Description |
---|---|---|
| The Mutex object to manage for the lifetime of this object. |
Table 4.852. Return Values for AutoLock::constructor()
Return Type | Description |
---|---|
AutoLock Object | The new AutoLock object. |
Calls Mutex::unlock() on the saved Mutex and destroys the AutoLock object.
delete lvalue
delete $al;
Throws an exception; objects of this class cannot be copied.
Table 4.853. Arguments for AutoLock::copy()
Argument | Type | Description |
---|---|---|
n/a | n/a | This method takes no arguments. |
Table 4.854. Return Values for AutoLock::copy()
Return Type | Description |
---|---|
n/a | This method returns no value because it throws an exception. |
Table 4.855. Exceptions Thrown by AutoLock::copy()
err | desc |
---|---|
| Objects of this class cannot be copied. |