Qore Programming Language Reference Manual: Qore::Thread::AutoWriteLock Class Reference

Qore Programming Language Reference Manual 0.8.11

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes

- Class List
- Class Index
- Class Hierarchy
- Class Members

All Classes Namespaces Functions Variables Groups Pages
Public Member Functions | List of all members
Qore::Thread::AutoWriteLock Class Reference

A helper class for the RWLock class for exception-safe write lock handling. More...

## Public Member Functions
| constructor (RWLock rwl)
| Creates the AutoWriteLock object based on the RWLock argument passed and immediately calls RWLock::writeLock() More...
| copy ()
| Throws an exception; objects of this class cannot be copied. More...
| destructor ()
| Calls RWLock::writeUnlock() on the saved RWLock and destroys the AutoWriteLock object. More...

## Detailed Description

A helper class for the RWLock class for exception-safe write lock handling.

Restrictions:
  Qore::PO_NO_THREAD_CLASSES

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 RWLock $rwl();
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 AutoWriteLock $awl($rwl);
if ($error)
throw "ERROR", "sorry, an error happened";
return "OK";
}

Note
  This class is not available with the PO_NO_THREAD_CLASSES parse option

## Member Function Documentation

Qore::Thread::AutoWriteLock::constructor | ( | RWLock | rwl | ) |

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

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

Example:
  my AutoWriteLock $awl($rwlock);

Exceptions

  THREAD-DEADLOCK | A deadlock was detected while trying to acquire the lock
  LOCK-ERROR | RWLock::writeLock() called while already holding the read lock, object deleted in another thread, etc.

Qore::Thread::AutoWriteLock::copy | ( |  | ) |

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

Exceptions

  AUTOWRITELOCK-COPY-ERROR | objects of this class cannot be copied

Qore::Thread::AutoWriteLock::destructor | ( |  | ) |

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

Example:
  delete $awl;

Exceptions

  LOCK-ERROR | RWLock::writeUnlock() called while not holding the write lock, RWLock object deleted in another thread, etc