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 \fInew\fR) 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 \fIcacheObject()\fR and \fIremoveFromCache()\fR functions. The cache is automatically deleted when the calling thread exits (either normally or via termination).
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 \fInot\fR delete per-thread data. TQThreadStorage only deletes per-thread data when the thread exits or when setLocalData() is called multiple times.
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 \fIcan\fR be used to store data for the \fImain()\fR thread \fIafter\fR QApplication has been constructed. TQThreadStorage deletes all data set for the \fImain()\fR thread when QApplication is destroyed, regardless of whether or not the \fImain()\fR 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.
Note: The per-thread data stored is \fInot\fR deleted. Any data left in TQThreadStorage is leaked. Make sure that all threads using TQThreadStorage have exited before deleting the TQThreadStorage.
Note: TQThreadStorage can only store pointers. This function returns a \fIreference\fR 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,
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.
If \fIdata\fR is non-zero, TQThreadStorage takes ownership of the \fIdata\fR 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 \fIdata\fR argument must be either a pointer to an object created on the heap (i.e. using \fInew\fR) or 0. You should not delete \fIdata\fR yourself; TQThreadStorage takes ownership and will delete the \fIdata\fR itself.