QThreadStorage Class▲
-
Header: QThreadStorage
-
CMake:
find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
-
qmake: QT += core
-
Group: QThreadStorage is part of thread
Detailed Description▲
https:// is a template class that provides per-thread data storage.
The https://() function stores a single thread-specific value for the calling thread. The data can be accessed later using https://().
The https://() function allows the programmer to determine if data has previously been set using the https://() function. This is also useful for lazy initialization.
If T is a pointer type, https:// 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 https:// 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)
{
caches.localData().insert(key, object);
}
void removeFromCache(const QString &key)
{
if (!caches.hasLocalData())
return;
caches.localData().remove(key);
}Caveats▲
-
The https:// destructor does not delete per-thread data. https:// only deletes per-thread data when the thread exits or when https://() is called multiple times.
-
https:// can be used to store data for the main() thread. https:// deletes all data set for the main() thread when QApplication is destroyed, regardless of whether or not the main() thread has actually finished.
See Also▲
See also https://
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 https:// is leaked. Make sure that all threads using https:// have exited before deleting the https://.
See Also▲
See also https://()
bool QThreadStorage::hasLocalData() const▲
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 https:// or https://.
See Also▲
See also https://()
T &QThreadStorage::localData()▲
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▲
See also https://(), https://()
T QThreadStorage::localData() const▲
This is an overloaded function.
Returns a copy of the data that was set by the calling thread.
See Also▲
See also https://()
void QThreadStorage::setLocalData(T data)▲
Sets the local data for the calling thread to data. It can be accessed later using the https://() functions.
If T is a pointer type, https:// 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▲
See also https://(), https://()


