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

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

Public Member Methods

 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)
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

◆ constructor()

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:
AutoWriteLock awl(rwlock);
Exceptions
THREAD-DEADLOCKA deadlock was detected while trying to acquire the lock
LOCK-ERRORRWLock::writeLock() called while already holding the read lock, object deleted in another thread, etc.

◆ copy()

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

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

Exceptions
AUTOWRITELOCK-COPY-ERRORobjects of this class cannot be copied

◆ destructor()

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

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

Example:
delete awl;
Exceptions
LOCK-ERRORRWLock::writeUnlock() called while not holding the write lock, RWLock object deleted in another thread, etc