Qt 4.8
Public Functions | Static Private Functions | Properties | List of all members
QThreadStorage< T > Class Template Reference

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

#include <qthreadstorage.h>

Public Functions

bool hasLocalData () const
 If T is a pointer type, returns true if the calling thread has non-zero data available. More...
 
T & localData ()
 Returns a reference to the data that was set by the calling thread. More...
 
localData () const
 Returns a copy of the data that was set by the calling thread. More...
 
 QThreadStorage ()
 Constructs a new per-thread data storage object. More...
 
void setLocalData (T t)
 Sets the local data for the calling thread to data. More...
 
 ~QThreadStorage ()
 Destroys the per-thread data storage object. More...
 

Static Private Functions

static void deleteData (void *x)
 

Properties

QThreadStorageData d
 

Detailed Description

template<class T>
class QThreadStorage< T >

The QThreadStorage class provides per-thread data storage.

Note
This class or function is threadsafe.

QThreadStorage is a template class that provides per-thread data storage.

The setLocalData() function stores a single thread-specific value for the calling thread. The data can be accessed later using localData().

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.

If T is a pointer type, 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.

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.

void cacheObject(const QString &key, SomeClass *object)
{
caches.localData().insert(key, object);
}
void removeFromCache(const QString &key)
{
if (!caches.hasLocalData())
return;
caches.localData().remove(key);
}

Caveats

See also
QThread

Definition at line 132 of file qthreadstorage.h.

Constructors and Destructors

◆ QThreadStorage()

template<class T>
QThreadStorage< T >::QThreadStorage ( )
inline

Constructs a new per-thread data storage object.

Definition at line 143 of file qthreadstorage.h.

143 : d(deleteData) { }
static void deleteData(void *x)
QThreadStorageData d

◆ ~QThreadStorage()

template<class T>
QThreadStorage< T >::~QThreadStorage ( )
inline

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

Definition at line 144 of file qthreadstorage.h.

144 { }

Functions

◆ deleteData()

template<class T>
static void QThreadStorage< T >::deleteData ( void *  x)
inlinestaticprivate

Definition at line 139 of file qthreadstorage.h.

140  { qThreadStorage_deleteData(x, reinterpret_cast<T*>(0)); }
void qThreadStorage_deleteData(void *d, T **)

◆ hasLocalData()

template<class T>
bool QThreadStorage< T >::hasLocalData ( ) const
inline

If T is a pointer type, returns true if the calling thread has non-zero data available.

If T is a value type, returns whether the data has already been constructed by calling setLocalData or localData.

See also
localData()

Definition at line 146 of file qthreadstorage.h.

Referenced by QFontCache::cleanup(), QUuid::createUuid(), and QHttpThreadDelegate::startRequest().

147  { return d.get() != 0; }
QThreadStorageData d
void ** get() const

◆ localData() [1/2]

template<class T>
T & QThreadStorage< T >::localData ( )
inline

Returns a reference to the data that was set by the calling thread.

If no data has been set, this will create a default constructed instance of type T.

See also
hasLocalData()

Definition at line 149 of file qthreadstorage.h.

Referenced by QIconvCodec::convertFromUnicode(), QPlatformGLContext::currentContext(), QPlatformGLContextPrivate::setCurrentContext(), QGLShaderStorage::shadersForThread(), sharedNetworkSessionManager(), QHttpThreadDelegate::startRequest(), and QHttpThreadDelegate::startRequestSynchronously().

150  { return qThreadStorage_localData(d, reinterpret_cast<T*>(0)); }
T *& qThreadStorage_localData(QThreadStorageData &d, T **)
QThreadStorageData d

◆ localData() [2/2]

template<class T>
const T QThreadStorage< T >::localData ( ) const
inline

Returns a copy of the data that was set by the calling thread.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

See also
hasLocalData()

Definition at line 151 of file qthreadstorage.h.

152  { return qThreadStorage_localData_const(d, reinterpret_cast<T*>(0)); }
QThreadStorageData d
T * qThreadStorage_localData_const(const QThreadStorageData &d, T **)

◆ setLocalData()

template<class T>
void QThreadStorage< T >::setLocalData ( data)
inline

Sets the local data for the calling thread to data.

It can be accessed later using the localData() functions.

If T is a pointer type, 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.

See also
localData(), hasLocalData()

Definition at line 154 of file qthreadstorage.h.

Referenced by QFontCache::cleanup(), QUuid::createUuid(), QPlatformGLContextPrivate::setCurrentContext(), sharedNetworkSessionManager(), QHttpThreadDelegate::startRequest(), and QHttpThreadDelegate::startRequestSynchronously().

QThreadStorageData d
void qThreadStorage_setLocalData(QThreadStorageData &d, T **t)

Properties

◆ d

template<class T>
QThreadStorageData QThreadStorage< T >::d
private

Definition at line 135 of file qthreadstorage.h.


The documentation for this class was generated from the following files: