Qt 4.8
|
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... | |
T | 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 |
The QThreadStorage class provides per-thread data storage.
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.
The QThreadStorage 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 be used to store data for the main()
thread. QThreadStorage deletes all data set for the main()
thread when QApplication is destroyed, regardless of whether or not the main()
thread has actually finished.
Definition at line 132 of file qthreadstorage.h.
|
inline |
|
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.
Definition at line 144 of file qthreadstorage.h.
|
inlinestaticprivate |
Definition at line 139 of file qthreadstorage.h.
|
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.
Definition at line 146 of file qthreadstorage.h.
Referenced by QFontCache::cleanup(), QUuid::createUuid(), and QHttpThreadDelegate::startRequest().
|
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.
Definition at line 149 of file qthreadstorage.h.
Referenced by QIconvCodec::convertFromUnicode(), QPlatformGLContext::currentContext(), QPlatformGLContextPrivate::setCurrentContext(), QGLShaderStorage::shadersForThread(), sharedNetworkSessionManager(), QHttpThreadDelegate::startRequest(), and QHttpThreadDelegate::startRequestSynchronously().
|
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.
Definition at line 151 of file qthreadstorage.h.
|
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.
Definition at line 154 of file qthreadstorage.h.
Referenced by QFontCache::cleanup(), QUuid::createUuid(), QPlatformGLContextPrivate::setCurrentContext(), sharedNetworkSessionManager(), QHttpThreadDelegate::startRequest(), and QHttpThreadDelegate::startRequestSynchronously().
|
private |
Definition at line 135 of file qthreadstorage.h.