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  ·  Classes principales  ·  Annotées  ·  Classes groupées  ·  Fonctions  · 

QThreadStorage Class Reference

The QThreadStorage class provides per-thread data storage. More...

All the functions in this class are thread-safe when Qt is built with thread support.

#include <qthreadstorage.h>

List of all member functions.

Public Members


Detailed Description

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 the localData() functions. 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 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 (either normally or via termination).

    QThreadStorage<QCache<SomeClass> *> caches;

    void cacheObject( const QString &key, SomeClass *object )
    {
        if ( ! caches.hasLocalData() )
            caches.setLocalData( new QCache<SomeClass> );

        caches.localData()->insert( key, object );
    }

    void removeFromCache( const QString &key )
    {
        if ( ! caches.hasLocalData() )
            return; // nothing to do

        caches.localData()->remove( key );
    }
    

Caveats

  • As noted above, QThreadStorage can only store pointers due to compiler limitations. Support for value-based objects will be added when the majority of compilers are able to support partial template specialization.

  • The destructor does not delete per-thread data. QThreadStorage only deletes per-thread data when the thread exits or when setLocalData() is called multiple times.

  • QThreadStorage can only be used with threads started with QThread. It cannot be used with threads started with platform-specific APIs.

  • As a corollary to the above, platform-specific APIs cannot be used to exit or terminate a QThread using QThreadStorage. Doing so will cause all per-thread data to be leaked. See QThread::exit() and QThread::terminate().

  • QThreadStorage can be used to store data for the main() thread after QApplication has been constructed. QThreadStorage deletes all data set for the main() thread when QApplication is destroyed, regardless of whether or not the main() thread has actually finished.

  • The implementation of QThreadStorage limits the total number of QThreadStorage objects to 256. An unlimited number of threads can store per-thread data in each QThreadStorage object.

See also Environment Classes and Threading.


Member Function Documentation

QThreadStorage::QThreadStorage ()

Constructs a new per-thread data storage object.

QThreadStorage::~QThreadStorage ()

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().

bool QThreadStorage::hasLocalData () const

Returns TRUE if the calling thread has non-zero data available; otherwise returns FALSE.

See also localData().

T & QThreadStorage::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 hasLocalData().

const T QThreadStorage::localData () const

This is an overloaded member function, provided for convenience. It behaves essentially like the above 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().

void QThreadStorage::setLocalData ( T data )

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().


This file is part of the Qt toolkit. Copyright © 1995-2003 Trolltech. All Rights Reserved.

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 94
  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. Pourquoi les programmeurs sont-ils moins payés que les gestionnaires de programmes ? Manquent-ils de pouvoir de négociation ? 42
  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 9
Page suivante

Le Qt Labs au hasard

Logo

Velours et QML Scene Graph

Les Qt Labs sont les laboratoires des développeurs de Qt, où ils peuvent partager des impressions sur le framework, son utilisation, ce que pourrait être son futur. 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 3.2
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