4.21. Thread::AutoWriteLock Class

Note: This class is not available with the PO_NO_THREAD_CLASSES parse option.

AutoWriteLock objects, when used along with a RWLock object, allow Qore programmers to safely acquire and release a write lock, even if exceptions are thrown or return statements are executed in the block where the AutoWriteLock object is created. AutoWriteLock objects are helper objects that acquire a write lock for the lifetime of the AutoWriteLock object. For this reason, it is only appropriate to assign an AutoWriteLock object to a local variable, so when the local variable goes out of scope, the AutoWriteLock object will be deleted and the write lock will be automatically released.

For example:

our $rwl = new RWLock();

sub check_error($error)
{
    # note that the write lock is acquired in the AutoWriteLock constructor, and
    # the write lock will be released as soon as the block is exited below.
    # (with either the throw statement or the return statement)
    my $arl = new AutoWriteLock($rwl);
    if ($error)
        throw "ERROR", "sorry, an error happened";

    return "OK";
}

Table 4.862. AutoWriteLock Method Overview

Method

Except?

Description

AutoWriteLock::constructor()

Y

Creates the AutoWriteLock object based on the RWLock argument passed and immediately calls RWLock::writeLock().

AutoWriteLock::destructor()

Y

Calls RWLock::writeUnlock() on the saved RWLock and destroys the AutoWriteLock object.

AutoWriteLock::copy()

Y

Throws an exception; objects of this class cannot be copied.


4.21.1. AutoWriteLock::constructor()

Synopsis

Creates the AutoWriteLock object based on the RWLock argument passed. The AutoWriteLock object immediately calls RWLock::writeLock() on the RWLock object passed, and saves it so it can be released when the AutoWriteLock object is destroyed.

Usage
new AutoWriteLock(rwlock_object)
Example
my $awl = new AutoWriteLock($rwlock);

Table 4.863. Arguments for AutoWriteLock::constructor()

Argument

Type

Description

$rwlock

RWLock

The RWLock object to manage for the lifetime of this object.


Table 4.864. Return Values for AutoWriteLock::constructor()

Return Type

Description

AutoWriteLock Object

The new AutoWriteLock object.


4.21.2. AutoWriteLock::destructor()

Synopsis

Calls RWLock::writeUnlock() on the saved RWLock and destroys the AutoWriteLock object.

Usage
delete lvalue
Example
delete $awl;

4.21.3. AutoWriteLock::copy()

Synopsis

Throws an exception; objects of this class cannot be copied.

Table 4.865. Arguments for AutoWriteLock::copy()

Argument

Type

Description

n/a

n/a

This method takes no arguments.


Table 4.866. Return Values for AutoWriteLock::copy()

Return Type

Description

n/a

This method returns no value because it throws an exception.


Table 4.867. Exceptions Thrown by AutoWriteLock::copy()

err

desc

AUTOWRITELOCK-COPY-ERROR

Objects of this class cannot be copied.