Qt 4.8
|
The QReadWriteLock class provides read-write locking. More...
#include <qreadwritelock.h>
Public Types | |
enum | RecursionMode { NonRecursive, Recursive } |
QReadWriteLock multiple times and the mutex won't be unlocked until a corresponding number of unlock() calls have been made. More... | |
Public Functions | |
void | lockForRead () |
Locks the lock for reading. More... | |
void | lockForWrite () |
Locks the lock for writing. More... | |
QReadWriteLock () | |
Constructs a QReadWriteLock object in NonRecursive mode. More... | |
QReadWriteLock (RecursionMode recursionMode) | |
Constructs a QReadWriteLock object in the given recursionMode. More... | |
bool | tryLockForRead () |
Attempts to lock for reading. More... | |
bool | tryLockForRead (int timeout) |
Attempts to lock for reading. More... | |
bool | tryLockForWrite () |
Attempts to lock for writing. More... | |
bool | tryLockForWrite (int timeout) |
Attempts to lock for writing. More... | |
void | unlock () |
Unlocks the lock. More... | |
~QReadWriteLock () | |
Destroys the QReadWriteLock object. More... | |
Properties | |
QReadWriteLockPrivate * | d |
Friends | |
class | QWaitCondition |
The QReadWriteLock class provides read-write locking.
A read-write lock is a synchronization tool for protecting resources that can be accessed for reading and writing. This type of lock is useful if you want to allow multiple threads to have simultaneous read-only access, but as soon as one thread wants to write to the resource, all other threads must be blocked until the writing is complete.
In many cases, QReadWriteLock is a direct competitor to QMutex. QReadWriteLock is a good choice if there are many concurrent reads and writing occurs infrequently.
Example:
To ensure that writers aren't blocked forever by readers, readers attempting to obtain a lock will not succeed if there is a blocked writer waiting for access, even if the lock is currently only accessed by other readers. Also, if the lock is accessed by a writer and another writer comes in, that writer will have priority over any readers that might also be waiting.
Like QMutex, a QReadWriteLock can be recursively locked by the same thread when constructed in QReadWriteLock::RecursionMode. In such cases, unlock() must be called the same number of times lockForWrite() or lockForRead() was called. Note that the lock type cannot be changed when trying to lock recursively, i.e. it is not possible to lock for reading in a thread that already has locked for writing (and vice versa).
Definition at line 58 of file qreadwritelock.h.
QReadWriteLock multiple times and the mutex won't be unlocked until a corresponding number of unlock() calls have been made.
Enumerator | |
---|---|
NonRecursive | |
Recursive |
Definition at line 61 of file qreadwritelock.h.
QReadWriteLock::QReadWriteLock | ( | ) |
Constructs a QReadWriteLock object in NonRecursive mode.
Definition at line 120 of file qreadwritelock.cpp.
QReadWriteLock::QReadWriteLock | ( | RecursionMode | recursionMode | ) |
Constructs a QReadWriteLock object in the given recursionMode.
Definition at line 134 of file qreadwritelock.cpp.
QReadWriteLock::~QReadWriteLock | ( | ) |
Destroys the QReadWriteLock object.
Definition at line 144 of file qreadwritelock.cpp.
void QReadWriteLock::lockForRead | ( | ) |
Locks the lock for reading.
This function will block the current thread if any thread (including the current) has locked for writing.
Definition at line 156 of file qreadwritelock.cpp.
Referenced by QSqlDatabasePrivate::database(), QWaitConditionPrivate::post(), QReadLocker::relock(), and QWaitCondition::wait().
void QReadWriteLock::lockForWrite | ( | ) |
Locks the lock for writing.
This function will block the current thread if another thread has locked for reading or writing.
Definition at line 287 of file qreadwritelock.cpp.
Referenced by QWaitConditionPrivate::post(), QWriteLocker::relock(), and QWaitCondition::wait().
bool QReadWriteLock::tryLockForRead | ( | ) |
Attempts to lock for reading.
If the lock was obtained, this function returns true, otherwise it returns false instead of waiting for the lock to become available, i.e. it does not block.
The lock attempt will fail if another thread has locked for writing.
If the lock was obtained, the lock must be unlocked with unlock() before another thread can successfully lock it.
Definition at line 199 of file qreadwritelock.cpp.
bool QReadWriteLock::tryLockForRead | ( | int | timeout | ) |
Attempts to lock for reading.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
This function returns true if the lock was obtained; otherwise it returns false. If another thread has locked for writing, this function will wait for at most timeout milliseconds for the lock to become available.
Note: Passing a negative number as the timeout is equivalent to calling lockForRead(), i.e. this function will wait forever until lock can be locked for reading when timeout is negative.
If the lock was obtained, the lock must be unlocked with unlock() before another thread can successfully lock it.
Definition at line 247 of file qreadwritelock.cpp.
bool QReadWriteLock::tryLockForWrite | ( | ) |
Attempts to lock for writing.
If the lock was obtained, this function returns true; otherwise, it returns false immediately.
The lock attempt will fail if another thread has locked for reading or writing.
If the lock was obtained, the lock must be unlocked with unlock() before another thread can successfully lock it.
Definition at line 327 of file qreadwritelock.cpp.
bool QReadWriteLock::tryLockForWrite | ( | int | timeout | ) |
Attempts to lock for writing.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
This function returns true if the lock was obtained; otherwise it returns false. If another thread has locked for reading or writing, this function will wait for at most timeout milliseconds for the lock to become available.
Note: Passing a negative number as the timeout is equivalent to calling lockForWrite(), i.e. this function will wait forever until lock can be locked for writing when timeout is negative.
If the lock was obtained, the lock must be unlocked with unlock() before another thread can successfully lock it.
Definition at line 374 of file qreadwritelock.cpp.
void QReadWriteLock::unlock | ( | ) |
Unlocks the lock.
Attempting to unlock a lock that is not locked is an error, and will result in program termination.
Definition at line 416 of file qreadwritelock.cpp.
Referenced by QSqlDatabasePrivate::database(), QWaitConditionPrivate::post(), QWSDisplay::ungrab(), QReadLocker::unlock(), QWriteLocker::unlock(), and QWaitCondition::wait().
|
friend |
Definition at line 81 of file qreadwritelock.h.
|
private |
Definition at line 79 of file qreadwritelock.h.
Referenced by lockForRead(), lockForWrite(), QWaitConditionPrivate::post(), tryLockForRead(), tryLockForWrite(), unlock(), QWaitCondition::wait(), and ~QReadWriteLock().