TQThreadStorage(3qt)

NAME
TQThreadStorage - Per-thread data storage

SYNOPSIS
All the functions in this class are thread-safe when TQt is built with thread support.</p>

#include <tqthreadstorage.h>

  Public Members
TQThreadStorage ()
~TQThreadStorage ()
bool hasLocalData () const
T & localData ()
T localData () const
void setLocalData ( T data )

DESCRIPTION
The TQThreadStorage class provides per-thread data storage.

TQThreadStorage is a template class that provides per-thread data storage.

Note that due to compiler limitations, TQThreadStorage can only store pointers.

The setLocalData() function stores a single thread-specific value for the calling thread. The data can be accessed later using the localData() functions. TQThreadStorage 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).

The hasLocalData() function allows the programmer to determine if data has previously been set using the setLocalData() function. This is useful for lazy initializiation.

For example, the following code uses TQThreadStorage 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).

    TQThreadStorage<TQCache<SomeClass> *> caches;
    void cacheObject( const TQString &key, SomeClass *object )
    {
        if ( ! caches.hasLocalData() )
            caches.setLocalData( new TQCache<SomeClass> );
        caches.localData()->insert( key, object );
    }
    void removeFromCache( const TQString &key )
    {
        if ( ! caches.hasLocalData() )
            return; // nothing to do
        caches.localData()->remove( key );
    }

Caveats

As noted above, TQThreadStorage 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.

The destructor does not delete per-thread data. TQThreadStorage only deletes per-thread data when the thread exits or when setLocalData() is called multiple times.

TQThreadStorage can only be used with threads started with TQThread. It cannot be used with threads started with platform-specific APIs.

As a corollary to the above, platform-specific APIs cannot be used to exit or terminate a TQThread using TQThreadStorage. Doing so will cause all per-thread data to be leaked. See TQThread::exit() and TQThread::terminate().

TQThreadStorage can be used to store data for the main() thread after TQApplication has been constructed. TQThreadStorage deletes all data set for the main() thread when TQApplication is destroyed, regardless of whether or not the main() thread has actually finished.

The implementation of TQThreadStorage limits the total number of TQThreadStorage objects to 256. An unlimited number of threads can store per-thread data in each TQThreadStorage object.

See also Environment Classes and Threading.

MEMBER FUNCTION DOCUMENTATION

TQThreadStorage::TQThreadStorage ()
Constructs a new per-thread data storage object.

TQThreadStorage::~TQThreadStorage ()
Destroys the per-thread data storage object.

Note: The per-thread data stored is not deleted. Any data left in TQThreadStorage is leaked. Make sure that all threads using TQThreadStorage have exited before deleting the TQThreadStorage.

See also hasLocalData().

bool TQThreadStorage::hasLocalData () const
Returns true if the calling thread has non-zero data available; otherwise returns false.

See also localData().

T & TQThreadStorage::localData ()
Returns a reference to the data that was set by the calling thread.

Note: TQThreadStorage can only store pointers. This function returns a reference to the pointer that was set by the calling thread. The value of this reference is 0 if no data was set by the calling thread,

See also hasLocalData().

T TQThreadStorage::localData () const
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Returns a copy of the data that was set by the calling thread.

Note: TQThreadStorage can 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 returns 0.

See also hasLocalData().

void TQThreadStorage::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.

If data is non-zero, TQThreadStorage 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: TQThreadStorage can only store pointers. The data argument must be either a pointer to an object created on the heap (i.e. using new) or 0. You should not delete data yourself; TQThreadStorage takes ownership and will delete the data itself.

See also localData() and hasLocalData().

SEE ALSO
http://doc.trolltech.com/tqthreadstorage.html http://www.trolltech.com/faq/tech.html

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.

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.

The definitive TQt documentation is provided in HTML format; it is located at $TQTDIR/doc/html and can be read using TQt 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 (tqthreadstorage.3qt) and the Qt version (3.3.8).