QThreadStorage Class Reference |
QThreadStorage () | |
~QThreadStorage () | |
bool | hasLocalData () const |
T & | localData () |
T | localData () const |
void | setLocalData ( T data ) |
The QThreadStorage class provides per-thread data storage.
QThreadStorage is a template class that provides per-thread data storage.
Note that due to compiler limitations, QThreadStorage can only store pointers.
The setLocalData() function stores a single thread-specific value for the calling thread. The data can be accessed later using localData(). QThreadStorage takes ownership of the data (which must be created on the heap with new) and deletes it when the thread exits, either normally or via termination.
The hasLocalData() function allows the programmer to determine if data has previously been set using the setLocalData() function. This is also useful for lazy initializiation.
For example, the following code uses QThreadStorage to store a single cache for each thread that calls the cacheObject() and removeFromCache() functions. The cache is automatically deleted when the calling thread exits.
QThreadStorage<QCache<QString, SomeClass> *> caches; void cacheObject(const QString &key, SomeClass *object) { if (!caches.hasLocalData()) caches.setLocalData(new QCache<QString, SomeClass>); caches.localData()->insert(key, object); } void removeFromCache(const QString &key) { if (!caches.hasLocalData()) return; caches.localData()->remove(key); }
See also QThread.
Constructs a new per-thread data storage object.
Destroys the per-thread data storage object.
Note: The per-thread data stored is not deleted. Any data left in QThreadStorage is leaked. Make sure that all threads using QThreadStorage have exited before deleting the QThreadStorage.
See also hasLocalData().
Returns true if the calling thread has non-zero data available; otherwise returns false.
See also localData().
Returns a reference to the data that was set by the calling thread.
Note: QThreadStorage can only store pointers. This function returns a reference to the pointer that was set by the calling thread. The value of this reference is 0 if no data was set by the calling thread,
See also setLocalData() and hasLocalData().
This is an overloaded function.
Returns a copy of the data that was set by the calling thread.
Note: QThreadStorage can only store pointers. This function returns a pointer to the data that was set by the calling thread. If no data was set by the calling thread, this function returns 0.
See also hasLocalData().
Sets the local data for the calling thread to data. It can be accessed later using the localData() functions.
If data is 0, this function deletes the previous data (if any) and returns immediately.
If data is non-zero, QThreadStorage takes ownership of the data and deletes it automatically either when the thread exits (either normally or via termination) or when setLocalData() is called again.
Note: QThreadStorage can only store pointers. The data argument must be either a pointer to an object created on the heap (i.e. using new) or 0. You should not delete data yourself; QThreadStorage takes ownership and will delete the data itself.
See also localData() and hasLocalData().
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 ! |
Copyright © 2000-2012 - www.developpez.com