Qt 4.8
Public Functions | Properties | List of all members
QReadLocker Class Reference

The QReadLocker class is a convenience class that simplifies locking and unlocking read-write locks for read access. More...

#include <qreadwritelock.h>

Public Functions

 QReadLocker (QReadWriteLock *readWriteLock)
 Constructs a QReadLocker and locks lock for reading. More...
 
QReadWriteLockreadWriteLock () const
 Returns a pointer to the read-write lock that was passed to the constructor. More...
 
void relock ()
 Relocks an unlocked lock. More...
 
void unlock ()
 Unlocks the lock associated with this locker. More...
 
 ~QReadLocker ()
 Destroys the QReadLocker and unlocks the lock that was passed to the constructor. More...
 

Properties

quintptr q_val
 

Detailed Description

The QReadLocker class is a convenience class that simplifies locking and unlocking read-write locks for read access.

Note
This class or function is threadsafe.

The purpose of QReadLocker (and QWriteLocker) is to simplify QReadWriteLock locking and unlocking. Locking and unlocking statements or in exception handling code is error-prone and difficult to debug. QReadLocker can be used in such situations to ensure that the state of the lock is always well-defined.

Here's an example that uses QReadLocker to lock and unlock a read-write lock for reading:

QByteArray readData()
{
QReadLocker locker(&lock);
...
return data;
}

It is equivalent to the following code:

QByteArray readData()
{
lock.lockForRead();
...
lock.unlock();
return data;
}

The QMutexLocker documentation shows examples where the use of a locker object greatly simplifies programming.

See also
QWriteLocker, QReadWriteLock

Definition at line 89 of file qreadwritelock.h.

Constructors and Destructors

◆ QReadLocker()

QReadLocker::QReadLocker ( QReadWriteLock lock)
inline

Constructs a QReadLocker and locks lock for reading.

The lock will be unlocked when the QReadLocker is destroyed. If lock is zero, QReadLocker does nothing.

See also
QReadWriteLock::lockForRead()

Definition at line 125 of file qreadwritelock.h.

126  : q_val(reinterpret_cast<quintptr>(areadWriteLock))
127 {
128  Q_ASSERT_X((q_val & quintptr(1u)) == quintptr(0),
129  "QReadLocker", "QReadWriteLock pointer is misaligned");
130  relock();
131 }
QIntegerForSizeof< void * >::Unsigned quintptr
Definition: qglobal.h:986
quint16 u
quintptr q_val
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
void relock()
Relocks an unlocked lock.

◆ ~QReadLocker()

QReadLocker::~QReadLocker ( )
inline

Destroys the QReadLocker and unlocks the lock that was passed to the constructor.

See also
QReadWriteLock::unlock()

Definition at line 94 of file qreadwritelock.h.

95  { unlock(); }
void unlock()
Unlocks the lock associated with this locker.

Functions

◆ readWriteLock()

QReadWriteLock * QReadLocker::readWriteLock ( ) const
inline

Returns a pointer to the read-write lock that was passed to the constructor.

Definition at line 117 of file qreadwritelock.h.

118  { return reinterpret_cast<QReadWriteLock *>(q_val & ~quintptr(1u)); }
QIntegerForSizeof< void * >::Unsigned quintptr
Definition: qglobal.h:986
quint16 u
quintptr q_val
The QReadWriteLock class provides read-write locking.

◆ relock()

void QReadLocker::relock ( )
inline

Relocks an unlocked lock.

See also
unlock()

Definition at line 107 of file qreadwritelock.h.

108  {
109  if (q_val) {
110  if ((q_val & quintptr(1u)) == quintptr(0u)) {
112  q_val |= quintptr(1u);
113  }
114  }
115  }
QIntegerForSizeof< void * >::Unsigned quintptr
Definition: qglobal.h:986
quint16 u
QReadWriteLock * readWriteLock() const
Returns a pointer to the read-write lock that was passed to the constructor.
void lockForRead()
Locks the lock for reading.
quintptr q_val

◆ unlock()

void QReadLocker::unlock ( )
inline

Unlocks the lock associated with this locker.

See also
QReadWriteLock::unlock()

Definition at line 97 of file qreadwritelock.h.

Referenced by QDeclarativeMetaType::interfaceIId().

98  {
99  if (q_val) {
100  if ((q_val & quintptr(1u)) == quintptr(1u)) {
101  q_val &= ~quintptr(1u);
102  readWriteLock()->unlock();
103  }
104  }
105  }
QIntegerForSizeof< void * >::Unsigned quintptr
Definition: qglobal.h:986
void unlock()
Unlocks the lock.
quint16 u
QReadWriteLock * readWriteLock() const
Returns a pointer to the read-write lock that was passed to the constructor.
quintptr q_val

Properties

◆ q_val

quintptr QReadLocker::q_val
private

Definition at line 122 of file qreadwritelock.h.


The documentation for this class was generated from the following files: