Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Toutes les fonctions  ·  Vues d'ensemble  · 

QReadWriteLock Class Reference
[QtCore module]

The QReadWriteLock class provides read-write locking. More...

 #include <QReadWriteLock>

Note: All functions in this class are thread-safe.


Public Types

enum RecursionMode { Recursive, NonRecursive }

Public Functions

QReadWriteLock ()
QReadWriteLock ( RecursionMode recursionMode )
~QReadWriteLock ()
void lockForRead ()
void lockForWrite ()
bool tryLockForRead ()
bool tryLockForRead ( int timeout )
bool tryLockForWrite ()
bool tryLockForWrite ( int timeout )
void unlock ()

Detailed Description

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;

 void ReaderThread::run()
 {
     ...
     lock.lockForRead();
     read_file();
     lock.unlock();
     ...
 }

 void WriterThread::run()
 {
     ...
     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. 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).

See also QReadLocker, QWriteLocker, QMutex, and QSemaphore.


Member Type Documentation

enum QReadWriteLock::RecursionMode

ConstantValueDescription
QReadWriteLock::Recursive1In this mode, a thread can lock the same QReadWriteLock multiple times and the mutex won't be unlocked until a corresponding number of unlock() calls have been made.
QReadWriteLock::NonRecursive0In this mode, a thread may only lock a QReadWriteLock once.

This enum was introduced in Qt 4.4.

See also QReadWriteLock().


Member Function Documentation

QReadWriteLock::QReadWriteLock ()

Constructs a QReadWriteLock object in NonRecursive mode.

See also lockForRead() and lockForWrite().

QReadWriteLock::QReadWriteLock ( RecursionMode recursionMode )

Constructs a QReadWriteLock object in the given recursionMode.

This function was introduced in Qt 4.4.

See also lockForRead(), lockForWrite(), and RecursionMode.

QReadWriteLock::~QReadWriteLock ()

Destroys the QReadWriteLock object.

Warning: Destroying a read-write lock that is in use may result in undefined behavior.

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.

See also unlock(), lockForWrite(), and tryLockForRead().

void QReadWriteLock::lockForWrite ()

Locks the lock for writing. This function will block the current thread if another thread has locked for reading or writing.

See also unlock(), lockForRead(), and tryLockForWrite().

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.

See also unlock() and lockForRead().

bool QReadWriteLock::tryLockForRead ( int timeout )

This is an overloaded function.

Attempts to lock for reading. 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.

See also unlock() and lockForRead().

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.

See also unlock() and lockForWrite().

bool QReadWriteLock::tryLockForWrite ( int timeout )

This is an overloaded function.

Attempts to lock for writing. 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.

See also unlock() and lockForWrite().

void QReadWriteLock::unlock ()

Unlocks the lock.

Attempting to unlock a lock that is not locked is an error, and will result in program termination.

See also lockForRead(), lockForWrite(), tryLockForRead(), and tryLockForWrite().

Publicité

Best Of

Actualités les plus lues

Semaine
Mois
Année
  1. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 82
  2. Apercevoir la troisième dimension ou l'utilisation multithreadée d'OpenGL dans Qt, un article des Qt Quarterly traduit par Guillaume Belz 0
  3. Les développeurs ignorent-ils trop les failles découvertes dans leur code ? Prenez-vous en compte les remarques des autres ? 17
  4. BlackBerry 10 : premières images du prochain OS de RIM qui devrait intégrer des widgets et des tuiles inspirées de Windows Phone 0
  5. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
  6. Adieu qmake, bienvenue qbs : Qt Building Suite, un outil déclaratif et extensible pour la compilation de projets Qt 17
  7. 2017 : un quinquennat pour une nouvelle version du C++ ? Possible, selon Herb Sutter 6
Page suivante

Le Qt Quarterly au hasard

Logo

Les formes du pluriel dans les traductions

Qt Quarterly est la revue trimestrielle proposée par Nokia et à destination des développeurs Qt. Ces articles d'une grande qualité technique sont rédigés par des experts Qt. Lire l'article.

Communauté

Ressources

Liens utiles

Contact

  • Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

Qt dans le magazine

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 4.6-snapshot
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web