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

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

#include <qreadwritelock.h>

Public Functions

 QWriteLocker (QReadWriteLock *readWriteLock)
 Constructs a QWriteLocker and locks lock for writing. 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...
 
 ~QWriteLocker ()
 Destroys the QWriteLocker and unlocks the lock that was passed to the constructor. More...
 

Properties

quintptr q_val
 

Detailed Description

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

Note
This class or function is threadsafe.

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

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

void writeData(const QByteArray &data)
{
QWriteLocker locker(&lock);
...
}

It is equivalent to the following code:

void writeData(const QByteArray &data)
{
lock.lockForWrite();
...
lock.unlock();
}

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

See also
QReadLocker, QReadWriteLock

Definition at line 133 of file qreadwritelock.h.

Constructors and Destructors

◆ QWriteLocker()

QWriteLocker::QWriteLocker ( QReadWriteLock lock)
inline

Constructs a QWriteLocker and locks lock for writing.

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

See also
QReadWriteLock::lockForWrite()

Definition at line 170 of file qreadwritelock.h.

171  : q_val(reinterpret_cast<quintptr>(areadWriteLock))
172 {
173  Q_ASSERT_X((q_val & quintptr(1u)) == quintptr(0),
174  "QWriteLocker", "QReadWriteLock pointer is misaligned");
175  relock();
176 }
QIntegerForSizeof< void * >::Unsigned quintptr
Definition: qglobal.h:986
void relock()
Relocks an unlocked lock.
quint16 u
quintptr q_val
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837

◆ ~QWriteLocker()

QWriteLocker::~QWriteLocker ( )
inline

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

See also
QReadWriteLock::unlock()

Definition at line 138 of file qreadwritelock.h.

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

Functions

◆ readWriteLock()

QReadWriteLock * QWriteLocker::readWriteLock ( ) const
inline

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

Definition at line 161 of file qreadwritelock.h.

162  { 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 QWriteLocker::relock ( )
inline

Relocks an unlocked lock.

See also
unlock()

Definition at line 151 of file qreadwritelock.h.

152  {
153  if (q_val) {
154  if ((q_val & quintptr(1u)) == quintptr(0u)) {
156  q_val |= quintptr(1u);
157  }
158  }
159  }
QIntegerForSizeof< void * >::Unsigned quintptr
Definition: qglobal.h:986
quint16 u
void lockForWrite()
Locks the lock for writing.
quintptr q_val
QReadWriteLock * readWriteLock() const
Returns a pointer to the read-write lock that was passed to the constructor.

◆ unlock()

void QWriteLocker::unlock ( )
inline

Unlocks the lock associated with this locker.

See also
QReadWriteLock::unlock()

Definition at line 141 of file qreadwritelock.h.

Referenced by QDeclarativeTypePrivate::init().

142  {
143  if (q_val) {
144  if ((q_val & quintptr(1u)) == quintptr(1u)) {
145  q_val &= ~quintptr(1u);
146  readWriteLock()->unlock();
147  }
148  }
149  }
QIntegerForSizeof< void * >::Unsigned quintptr
Definition: qglobal.h:986
void unlock()
Unlocks the lock.
quint16 u
quintptr q_val
QReadWriteLock * readWriteLock() const
Returns a pointer to the read-write lock that was passed to the constructor.

Properties

◆ q_val

quintptr QWriteLocker::q_val
private

Definition at line 167 of file qreadwritelock.h.


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