|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.trolltech.qt.internal.QSignalEmitterInternal
com.trolltech.qt.QSignalEmitter
com.trolltech.qt.QtJambiObject
com.trolltech.qt.core.QReadWriteLock
public class QReadWriteLock
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:
QReadWriteLock lock = new QReadWriteLock(); public void readerFunction() { // ... lock.lockForRead(); read_file(); lock.unlock(); // ... } public void writerFunction() { // ... lock.lockForWrite(); write_file(); lock.unlock(); // ... }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
recursive mode}. 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).
QMutex
, and QSemaphore
.
Nested Class Summary | |
---|---|
static class |
QReadWriteLock.RecursionMode
See also. QReadWriteLock(). |
Nested classes/interfaces inherited from class com.trolltech.qt.QSignalEmitter |
---|
QSignalEmitter.AbstractSignal, QSignalEmitter.Signal0, QSignalEmitter.Signal1, QSignalEmitter.Signal2, QSignalEmitter.Signal3, QSignalEmitter.Signal4, QSignalEmitter.Signal5, QSignalEmitter.Signal6, QSignalEmitter.Signal7, QSignalEmitter.Signal8, QSignalEmitter.Signal9 |
Nested classes/interfaces inherited from class com.trolltech.qt.internal.QSignalEmitterInternal |
---|
com.trolltech.qt.internal.QSignalEmitterInternal.AbstractSignalInternal |
Field Summary |
---|
Fields inherited from class com.trolltech.qt.internal.QSignalEmitterInternal |
---|
currentSender |
Constructor Summary | |
---|---|
QReadWriteLock()
Constructs a QReadWriteLock object in NonRecursive mode. |
|
QReadWriteLock(QReadWriteLock.RecursionMode recursionMode)
Constructs a QReadWriteLock object in the given recursionMode. |
Method Summary | |
---|---|
void |
lockForRead()
Locks the lock for reading. |
void |
lockForWrite()
Locks the lock for writing. |
boolean |
tryLockForRead()
Attempts to lock for reading. |
boolean |
tryLockForRead(int timeout)
Attempts to lock for reading. |
boolean |
tryLockForWrite()
Attempts to lock for writing. |
boolean |
tryLockForWrite(int timeout)
Attempts to lock for writing. |
void |
unlock()
Unlocks the lock. |
Methods inherited from class com.trolltech.qt.QtJambiObject |
---|
dispose, disposed, equals, finalize, reassignNativeResources, tr, tr, tr |
Methods inherited from class com.trolltech.qt.QSignalEmitter |
---|
blockSignals, disconnect, disconnect, signalsBlocked, signalSender, thread |
Methods inherited from class com.trolltech.qt.internal.QSignalEmitterInternal |
---|
__qt_signalInitialization |
Methods inherited from class java.lang.Object |
---|
clone, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Methods inherited from interface com.trolltech.qt.QtJambiInterface |
---|
disableGarbageCollection, nativeId, nativePointer, reenableGarbageCollection, setJavaOwnership |
Constructor Detail |
---|
public QReadWriteLock()
QReadWriteLock
object in NonRecursive
mode. lockForRead()
, and lockForWrite()
.
public QReadWriteLock(QReadWriteLock.RecursionMode recursionMode)
QReadWriteLock
object in the given recursionMode. lockForRead()
, lockForWrite()
, and RecursionMode
.
Method Detail |
---|
public final void lockForRead()
unlock()
, lockForWrite()
, and tryLockForRead()
.
public final void lockForWrite()
unlock()
, lockForRead()
, and tryLockForWrite()
.
public final boolean tryLockForRead()
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.
unlock()
, and lockForRead()
.
public final boolean tryLockForRead(int timeout)
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.
unlock()
, and lockForRead()
.
public final boolean tryLockForWrite()
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.
unlock()
, and lockForWrite()
.
public final boolean tryLockForWrite(int timeout)
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.
unlock()
, and lockForWrite()
.
public final void unlock()
Attempting to unlock a lock that is not locked is an error, and will result in program termination.
lockForRead()
, lockForWrite()
, tryLockForRead()
, and tryLockForWrite()
.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |