1QThreadStorage(3qt)                                        QThreadStorage(3qt)
2
3
4

NAME

6       QThreadStorage - Per-thread data storage
7

SYNOPSIS

9       All the functions in this class are thread-safe when Qt is built with
10       thread support.</p>
11
12       #include <qthreadstorage.h>
13
14   Public Members
15       QThreadStorage ()
16       ~QThreadStorage ()
17       bool hasLocalData () const
18       T & localData ()
19       T localData () const
20       void setLocalData ( T data )
21

DESCRIPTION

23       The QThreadStorage class provides per-thread data storage.
24
25       QThreadStorage is a template class that provides per-thread data
26       storage.
27
28       Note that due to compiler limitations, QThreadStorage can only store
29       pointers.
30
31       The setLocalData() function stores a single thread-specific value for
32       the calling thread. The data can be accessed later using the
33       localData() functions. QThreadStorage takes ownership of the data
34       (which must be created on the heap with new) and deletes it when the
35       thread exits (either normally or via termination).
36
37       The hasLocalData() function allows the programmer to determine if data
38       has previously been set using the setLocalData() function. This is
39       useful for lazy initializiation.
40
41       For example, the following code uses QThreadStorage to store a single
42       cache for each thread that calls the cacheObject() and
43       removeFromCache() functions. The cache is automatically deleted when
44       the calling thread exits (either normally or via termination).
45
46           QThreadStorage<QCache<SomeClass> *> caches;
47           void cacheObject( const QString &key, SomeClass *object )
48           {
49               if ( ! caches.hasLocalData() )
50                   caches.setLocalData( new QCache<SomeClass> );
51               caches.localData()->insert( key, object );
52           }
53           void removeFromCache( const QString &key )
54           {
55               if ( ! caches.hasLocalData() )
56                   return; // nothing to do
57               caches.localData()->remove( key );
58           }
59

Caveats

61       As noted above, QThreadStorage can only store pointers due to compiler
62       limitations. Support for value-based objects will be added when the
63       majority of compilers are able to support partial template
64       specialization.
65
66       The destructor does not delete per-thread data. QThreadStorage only
67       deletes per-thread data when the thread exits or when setLocalData() is
68       called multiple times.
69
70       QThreadStorage can only be used with threads started with QThread. It
71       cannot be used with threads started with platform-specific APIs.
72
73       As a corollary to the above, platform-specific APIs cannot be used to
74       exit or terminate a QThread using QThreadStorage. Doing so will cause
75       all per-thread data to be leaked. See QThread::exit() and
76       QThread::terminate().
77
78       QThreadStorage can be used to store data for the main() thread after
79       QApplication has been constructed. QThreadStorage deletes all data set
80       for the main() thread when QApplication is destroyed, regardless of
81       whether or not the main() thread has actually finished.
82
83       The implementation of QThreadStorage limits the total number of
84       QThreadStorage objects to 256. An unlimited number of threads can store
85       per-thread data in each QThreadStorage object.
86
87       See also Environment Classes and Threading.
88

MEMBER FUNCTION DOCUMENTATION

QThreadStorage::QThreadStorage ()

91       Constructs a new per-thread data storage object.
92

QThreadStorage::~QThreadStorage ()

94       Destroys the per-thread data storage object.
95
96       Note: The per-thread data stored is not deleted. Any data left in
97       QThreadStorage is leaked. Make sure that all threads using
98       QThreadStorage have exited before deleting the QThreadStorage.
99
100       See also hasLocalData().
101

bool QThreadStorage::hasLocalData () const

103       Returns TRUE if the calling thread has non-zero data available;
104       otherwise returns FALSE.
105
106       See also localData().
107

T & QThreadStorage::localData ()

109       Returns a reference to the data that was set by the calling thread.
110
111       Note: QThreadStorage can only store pointers. This function returns a
112       reference to the pointer that was set by the calling thread. The value
113       of this reference is 0 if no data was set by the calling thread,
114
115       See also hasLocalData().
116

T QThreadStorage::localData () const

118       This is an overloaded member function, provided for convenience. It
119       behaves essentially like the above function.
120
121       Returns a copy of the data that was set by the calling thread.
122
123       Note: QThreadStorage can only store pointers. This function returns a
124       pointer to the data that was set by the calling thread. If no data was
125       set by the calling thread, this function returns 0.
126
127       See also hasLocalData().
128

void QThreadStorage::setLocalData ( T data )

130       Sets the local data for the calling thread to data. It can be accessed
131       later using the localData() functions.
132
133       If data is 0, this function deletes the previous data (if any) and
134       returns immediately.
135
136       If data is non-zero, QThreadStorage takes ownership of the data and
137       deletes it automatically either when the thread exits (either normally
138       or via termination) or when setLocalData() is called again.
139
140       Note: QThreadStorage can only store pointers. The data argument must be
141       either a pointer to an object created on the heap (i.e. using new) or
142       0. You should not delete data yourself; QThreadStorage takes ownership
143       and will delete the data itself.
144
145       See also localData() and hasLocalData().
146
147

SEE ALSO

149       http://doc.trolltech.com/qthreadstorage.html
150       http://www.trolltech.com/faq/tech.html
151
153       Copyright 1992-2007 Trolltech ASA, http://www.trolltech.com.  See the
154       license file included in the distribution for a complete license
155       statement.
156

AUTHOR

158       Generated automatically from the source code.
159

BUGS

161       If you find a bug in Qt, please report it as described in
162       http://doc.trolltech.com/bughowto.html.  Good bug reports help us to
163       help you. Thank you.
164
165       The definitive Qt documentation is provided in HTML format; it is
166       located at $QTDIR/doc/html and can be read using Qt Assistant or with a
167       web browser. This man page is provided as a convenience for those users
168       who prefer man pages, although this format is not officially supported
169       by Trolltech.
170
171       If you find errors in this manual page, please report them to qt-
172       bugs@trolltech.com.  Please include the name of the manual page
173       (qthreadstorage.3qt) and the Qt version (3.3.8).
174
175
176
177Trolltech AS                    2 February 2007            QThreadStorage(3qt)
Impressum