QThreadStorage(3qt)
π NAME π
okay bestie, welcome to QThreadStorage(3qt) β the page about per-thread data storage. that's literally it, that's the whole vibe. one thread, one lil stash of data, no sharing, no drama π
β¨ SYNOPSIS β¨
first things first, the glow-up disclaimer: all the functions in this class are thread-safe when Qt is built with thread support. </p>
#include <qthreadstorage.h>
π Public Members π
QThreadStorage ()~QThreadStorage ()bool hasLocalData () constT & localData ()T localData () constvoid setLocalData ( T data )
πΈ DESCRIPTION πΈ
The QThreadStorage class provides per-thread data storage. that's the whole main character arc right there.
QThreadStorage is a template class that provides per-thread data storage. periodt.
not me having to say this but β due to compiler limitations, QThreadStorage can only store pointers. like, only pointers. that's the rule. π
The setLocalData() function stores a single thread-specific value for the calling thread. The data can be accessed later using the localData() functions. 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. so it's giving responsible queen, it cleans up after itself π§Ήβ¨
The hasLocalData() function allows the programmer to determine if data has previously been set using the setLocalData() function. This is useful for lazy initializiation. (yes bestie, that spelling is straight from the source, we don't touch it π«Ά)
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 β either normally or via termination.
QThreadStorage<QCache<SomeClass> *> caches;
void cacheObject( const QString &key, SomeClass *object )
{
if ( ! caches.hasLocalData() )
caches.setLocalData( new QCache<SomeClass> );
caches.localData()->insert( key, object );
}
void removeFromCache( const QString &key )
{
if ( ! caches.hasLocalData() )
return; // nothing to do
caches.localData()->remove( key );
}
π Caveats π
this is the part where we sit down and have a serious little chat, bestie. grab a snack. π
As noted above, QThreadStorage can only store pointers due to compiler limitations. Support for value-based objects will be added when the majority of compilers are able to support partial template specialization. so likeβ¦ someday. not today. β³
The destructor does not delete per-thread data. QThreadStorage only deletes per-thread data when the thread exits or when setLocalData() is called multiple times. write that down. ποΈ
QThreadStorage can only be used with threads started with QThread. It cannot be used with threads started with platform-specific APIs. no cap.
As a corollary to the above, platform-specific APIs cannot be used to exit or terminate a QThread using QThreadStorage. Doing so will cause all per-thread data to be leaked. See QThread::exit() and QThread::terminate(). the leak is real and it is ugly π
QThreadStorage can be used to store data for the main() thread after QApplication has been constructed. QThreadStorage deletes all data set for the main() thread when QApplication is destroyed, regardless of whether or not the main() thread has actually finished. okay so it does NOT wait for you to be done, it just closes the door. π
The implementation of QThreadStorage limits the total number of QThreadStorage objects to 256. An unlimited number of threads can store per-thread data in each QThreadStorage object. 256 storage objects, infinite threads per object. slay math. π₯
See also Environment Classes and Threading.
π¦ MEMBER FUNCTION DOCUMENTATION π¦
QThreadStorage::QThreadStorage ()
Constructs a new per-thread data storage object. brand new, fresh out the box, no notes. β¨
QThreadStorage::~QThreadStorage ()
Destroys the per-thread data storage object.
Note: The per-thread data stored is not deleted. Any data left inQThreadStorageis leaked. Make sure that all threads usingQThreadStoragehave exited before deleting theQThreadStorage. π¨
See also hasLocalData().
bool QThreadStorage::hasLocalData () const
Returns TRUE if the calling thread has non-zero data available; otherwise returns FALSE. it's a vibe check for your data. π
See also localData().
T & QThreadStorage::localData ()
Returns a reference to the data that was set by the calling thread.
Note:QThreadStoragecan only store pointers. This function returns a reference to the pointer that was set by the calling thread. The value of this reference is0if no data was set by the calling thread,
See also hasLocalData().
T QThreadStorage::localData () const
This is an overloaded member function, provided for convenience. It behaves essentially like the above function. twins, basically. π«Ά
Returns a copy of the data that was set by the calling thread.
Note:QThreadStoragecan only store pointers. This function returns a pointer to the data that was set by the calling thread. If no data was set by the calling thread, this function returns0.
See also hasLocalData().
void QThreadStorage::setLocalData ( T data )
Sets the local data for the calling thread to data. It can be accessed later using the localData() functions.
If data is 0, this function deletes the previous data (if any) and returns immediately. bye bestie π
If data is non-zero, 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.
Note:QThreadStoragecan only store pointers. The data argument must be either a pointer to an object created on the heap (i.e. usingnew) or0. You should not delete data yourself;QThreadStoragetakes ownership and will delete the data itself. do NOT fight it for custody, it won the custody battle fair and square π€
See also localData() and hasLocalData().
π SEE ALSO π
π COPYRIGHT π
Copyright 1992-2007 Trolltech ASA, http://www.trolltech.com. See the license file included in the distribution for a complete license statement.
π AUTHOR π
Generated automatically from the source code. no human wrote this, it simply manifested. iconic behavior fr fr β¨
π BUGS π
If you find a bug in Qt, please report it as described in http://doc.trolltech.com/bughowto.html. Good bug reports help us to help you. Thank you. (we love a helpful bestie, we really do π«Ά)
The definitive Qt documentation is provided in HTML format; it is located at $QTDIR/doc/html and can be read using Qt Assistant or with a web browser. This man page is provided as a convenience for those users who prefer man pages, although this format is not officially supported by Trolltech.
If you find errors in this manual page, please report them to qt-bugs@trolltech.com. Please include the name of the manual page (qthreadstorage.3qt) and the Qt version (3.3.8). ty ty ty π
and that's the whole page bestie!! brought to you by Trolltech AS, dated 2 February 2007, and yeah the manual name is QThreadStorage(3qt) in case you forgot (you didn't, you're doing amazing). go forth and store your pointers per-thread. no cap. π
β¨π¦
π¦ where this page comes from
- fedora 43: qt3-devel-docs 3.3.8b-102.fc43 Β· QPL-1.0 OR GPL-2.0-only OR GPL-3.0-only
- fedora 44: qt3-devel-docs 3.3.8b-103.fc44 Β· QPL-1.0 OR GPL-2.0-only OR GPL-3.0-only
- Copyright 1992-2007 Trolltech ASA. All rights reserved. See the
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms