QThreadStorage Classtemplate <typename T> class QThreadStorage
The QThreadStorage class provides per-thread data storage. More...
|CMake:|| find_package(Qt6 COMPONENTS Core REQUIRED) |
target_link_libraries(mytarget PRIVATE Qt6::Core)
|qmake:||QT += core|
Note: All functions in this class are thread-safe.
QThreadStorage is a template class that provides per-thread data storage.
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.
See also QThread.
Member Function Documentation
Constructs a new per-thread data storage object.
Destroys the per-thread data storage object.
See also hasLocalData().
If T is a pointer type, returns
true if the calling thread has non-zero data available.
See also 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.
This is an overloaded function.
Returns a copy of the data that was set by the calling thread.
See also hasLocalData().
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.
© 2021 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.