![]() |
Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
|
\inmodule QtCore More...
#include <qthreadstorage.h>
Public Member Functions | |
| QThreadStorage ()=default | |
| Constructs a new per-thread data storage object. | |
| ~QThreadStorage ()=default | |
| Destroys the per-thread data storage object. | |
| QThreadStorage (const QThreadStorage &rhs)=delete | |
| QThreadStorage & | operator= (const QThreadStorage &rhs)=delete |
| bool | hasLocalData () const |
If T is a pointer type, returns true if the calling thread has non-zero data available. | |
| T & | localData () |
| Returns a reference to the data that was set by the calling thread. | |
| T | localData () const |
| This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Returns a copy of the data that was set by the calling thread. | |
| void | setLocalData (T t) |
| Sets the local data for the calling thread to data. | |
| void | clearLocalData () |
\inmodule QtCore
The QThreadStorage class provides per-thread data storage.
\threadsafe
QThreadStorage is a template class where the template parameter T specifies the type of data stored per-thread.
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 initialization.
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.
Definition at line 158 of file qthreadstorage.h.
|
default |
Constructs a new per-thread data storage object.
|
default |
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.
|
delete |
|
inline |
Resets the local data (if any) for the calling thread.
If the thread had no localData(), does nothing. After the call, hasLocalData() is {false} (unless the destructor of the previous value stored a new one).
{T} is a pointer type, this is mostly the same as {setLocalData(nullptr)}. For non-pointers, this functionality was not accessible before Qt 6.13.This function differs from setLocalData() in that it will not create thread-local storage when it doesn't exist for the current thread (yet, or anymore). This makes calling the function safe in shutdown code, where thread-local storage may already have been destroyed, and setLocalData() would re-create it with nothing left to destroy it afterwards.
Definition at line 200 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 178 of file qthreadstorage.h.
|
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 183 of file qthreadstorage.h.
|
inline |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Returns a copy of the data that was set by the calling thread.
Definition at line 190 of file qthreadstorage.h.
|
delete |
|
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 195 of file qthreadstorage.h.