Qore Programming Language Reference Manual  0.8.12.8
Qore::Thread::AutoReadLock Class Reference

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

Public Member Functions

 constructor (RWLock rwl)
 Creates the AutoReadLock object based on the RWLock argument passed and immediately calls RWLock::readLock() More...
 
 copy ()
 Throws an exception; objects of this class cannot be copied. More...
 
 destructor ()
 Calls RWLock::readUnlock() on the saved RWLock and destroys the AutoReadLock object. More...
 

Detailed Description

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

Restrictions:
Qore::PO_NO_THREAD_CLASSES

AutoReadLock objects, when used along with a RWLock object, allow Qore programmers to safely acquire and release a read lock, even if exceptions are thrown or return statements are executed in the block where the AutoReadLock object is created.

AutoReadLock objects are helper objects that acquire a read lock for the lifetime of the AutoReadLock object. For this reason, it is only appropriate to assign an AutoReadLock object to a local variable, so when the local variable goes out of scope, the AutoReadLock object will be deleted and the read lock will be automatically released.

For example:

our RWLock rwl();
sub check_error(error) {
# note that the read lock is acquired in the AutoReadLock constructor, and
# the read lock will be released as soon as the block is exited below.
# (with either the throw statement or the return statement)
AutoReadLock arl(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::AutoReadLock::constructor ( RWLock  rwl)

Creates the AutoReadLock object based on the RWLock argument passed and immediately calls RWLock::readLock()

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

Example:
AutoReadLock arl(rwlock);
Exceptions
THREAD-DEADLOCKA deadlock was detected while trying to acquire the lock
LOCK-ERRORRWLock::readLock() called while already holding the write lock, object deleted in another thread, etc.

◆ copy()

Qore::Thread::AutoReadLock::copy ( )

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

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

◆ destructor()

Qore::Thread::AutoReadLock::destructor ( )

Calls RWLock::readUnlock() on the saved RWLock and destroys the AutoReadLock object.

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