source: trunk/doc/html/qthreadstorage.html@ 190

Last change on this file since 190 was 190, checked in by rudi, 14 years ago

reference documentation added

File size: 8.7 KB
Line 
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/src/tools/qthreadstorage_unix.cpp:163 -->
3<html>
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6<title>QThreadStorage Class</title>
7<style type="text/css"><!--
8fn { margin-left: 1cm; text-indent: -1cm; }
9a:link { color: #004faf; text-decoration: none }
10a:visited { color: #672967; text-decoration: none }
11body { background: #ffffff; color: black; }
12--></style>
13</head>
14<body>
15
16<table border="0" cellpadding="0" cellspacing="0" width="100%">
17<tr bgcolor="#E5E5E5">
18<td valign=center>
19 <a href="index.html">
20<font color="#004faf">Home</font></a>
21 | <a href="classes.html">
22<font color="#004faf">All&nbsp;Classes</font></a>
23 | <a href="mainclasses.html">
24<font color="#004faf">Main&nbsp;Classes</font></a>
25 | <a href="annotated.html">
26<font color="#004faf">Annotated</font></a>
27 | <a href="groups.html">
28<font color="#004faf">Grouped&nbsp;Classes</font></a>
29 | <a href="functions.html">
30<font color="#004faf">Functions</font></a>
31</td>
32<td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>QThreadStorage Class Reference</h1>
33
34<p>The QThreadStorage class provides per-thread data storage.
35<a href="#details">More...</a>
36<p>All the functions in this class are <a href="threads.html#threadsafe">thread-safe</a> when Qt is built with thread support.</p>
37<p><tt>#include &lt;<a href="qthreadstorage-h.html">qthreadstorage.h</a>&gt;</tt>
38<p><a href="qthreadstorage-members.html">List of all member functions.</a>
39<h2>Public Members</h2>
40<ul>
41<li class=fn><a href="#QThreadStorage"><b>QThreadStorage</b></a> ()</li>
42<li class=fn><a href="#~QThreadStorage"><b>~QThreadStorage</b></a> ()</li>
43<li class=fn>bool <a href="#hasLocalData"><b>hasLocalData</b></a> () const</li>
44<li class=fn>T &amp; <a href="#localData"><b>localData</b></a> ()</li>
45<li class=fn>T <a href="#localData-2"><b>localData</b></a> () const</li>
46<li class=fn>void <a href="#setLocalData"><b>setLocalData</b></a> ( T&nbsp;data )</li>
47</ul>
48<hr><a name="details"></a><h2>Detailed Description</h2>
49
50
51The QThreadStorage class provides per-thread data storage.
52<p>
53
54
55<p> QThreadStorage is a template class that provides per-thread data
56storage.
57<p> <em>Note that due to compiler limitations, QThreadStorage can only store pointers.</em>
58<p> The <a href="#setLocalData">setLocalData</a>() function stores a single thread-specific value
59for the calling thread. The data can be accessed later using the
60<a href="#localData">localData</a>() functions. QThreadStorage takes ownership of the
61data (which must be created on the heap with <em>new</em>) and deletes
62it when the thread exits (either normally or via termination).
63<p> The <a href="#hasLocalData">hasLocalData</a>() function allows the programmer to determine if
64data has previously been set using the setLocalData() function.
65This is useful for lazy initializiation.
66<p> For example, the following code uses QThreadStorage to store a
67single cache for each thread that calls the <em>cacheObject()</em> and
68<em>removeFromCache()</em> functions. The cache is automatically
69deleted when the calling thread exits (either normally or via
70termination).
71<p> <pre>
72 QThreadStorage&lt;QCache&lt;SomeClass&gt; *&gt; caches;
73
74 void cacheObject( const <a href="qstring.html">QString</a> &amp;key, SomeClass *object )
75 {
76 if ( ! caches.hasLocalData() )
77 caches.setLocalData( new <a href="qcache.html">QCache</a>&lt;SomeClass&gt; );
78
79 caches.localData()-&gt;insert( key, object );
80 }
81
82 void removeFromCache( const <a href="qstring.html">QString</a> &amp;key )
83 {
84 if ( ! caches.hasLocalData() )
85 return; // nothing to do
86
87 caches.localData()-&gt;remove( key );
88 }
89 </pre>
90
91<p> <h3> Caveats
92</h3>
93<a name="1"></a><p> <ul>
94<p> <li> As noted above, QThreadStorage can only store pointers due to
95compiler limitations. Support for value-based objects will be
96added when the majority of compilers are able to support partial
97template specialization.
98<p> <li> The <a href="#~QThreadStorage">destructor</a> does <em>not</em>
99delete per-thread data. QThreadStorage only deletes per-thread
100data when the thread exits or when <a href="#setLocalData">setLocalData</a>() is called
101multiple times.
102<p> <li> QThreadStorage can only be used with threads started with
103<a href="qthread.html">QThread</a>. It <em>cannot</em> be used with threads started with
104platform-specific APIs.
105<p> <li> As a corollary to the above, platform-specific APIs cannot be
106used to exit or terminate a QThread using QThreadStorage. Doing so
107will cause all per-thread data to be leaked. See <a href="qthread.html#exit">QThread::exit</a>()
108and <a href="qthread.html#terminate">QThread::terminate</a>().
109<p> <li> QThreadStorage <em>can</em> be used to store data for the <em>main()</em>
110thread <em>after</em> <a href="qapplication.html">QApplication</a> has been constructed. QThreadStorage
111deletes all data set for the <em>main()</em> thread when QApplication is
112destroyed, regardless of whether or not the <em>main()</em> thread has
113actually finished.
114<p> <li> The implementation of QThreadStorage limits the total number of
115QThreadStorage objects to 256. An unlimited number of threads
116can store per-thread data in each QThreadStorage object.
117<p> </ul>
118<p>See also <a href="environment.html">Environment Classes</a> and <a href="thread.html">Threading</a>.
119
120<hr><h2>Member Function Documentation</h2>
121<h3 class=fn><a name="QThreadStorage"></a>QThreadStorage::QThreadStorage ()
122</h3>
123
124<p> Constructs a new per-thread data storage object.
125
126<h3 class=fn><a name="~QThreadStorage"></a>QThreadStorage::~QThreadStorage ()
127</h3>
128
129<p> Destroys the per-thread data storage object.
130<p> Note: The per-thread data stored is <em>not</em> deleted. Any data left
131in QThreadStorage is leaked. Make sure that all threads using
132QThreadStorage have exited before deleting the QThreadStorage.
133<p> <p>See also <a href="#hasLocalData">hasLocalData</a>().
134
135<h3 class=fn>bool <a name="hasLocalData"></a>QThreadStorage::hasLocalData () const
136</h3>
137
138<p> Returns TRUE if the calling thread has non-zero data available;
139otherwise returns FALSE.
140<p> <p>See also <a href="#localData">localData</a>().
141
142<h3 class=fn>T &amp; <a name="localData"></a>QThreadStorage::localData ()
143</h3>
144
145<p> Returns a reference to the data that was set by the calling
146thread.
147<p> Note: QThreadStorage can only store pointers. This function
148returns a <em>reference</em> to the pointer that was set by the calling
149thread. The value of this reference is 0 if no data was set by
150the calling thread,
151<p> <p>See also <a href="#hasLocalData">hasLocalData</a>().
152
153<h3 class=fn>T <a name="localData-2"></a>QThreadStorage::localData () const
154</h3>
155
156This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
157<p> Returns a copy of the data that was set by the calling thread.
158<p> Note: QThreadStorage can only store pointers. This function
159returns a pointer to the data that was set by the calling thread.
160If no data was set by the calling thread, this function returns 0.
161<p> <p>See also <a href="#hasLocalData">hasLocalData</a>().
162
163<h3 class=fn>void <a name="setLocalData"></a>QThreadStorage::setLocalData ( T&nbsp;data )
164</h3>
165
166<p> Sets the local data for the calling thread to <em>data</em>. It can be
167accessed later using the <a href="#localData">localData</a>() functions.
168<p> If <em>data</em> is 0, this function deletes the previous data (if
169any) and returns immediately.
170<p> If <em>data</em> is non-zero, QThreadStorage takes ownership of the <em>data</em> and deletes it automatically either when the thread exits
171(either normally or via termination) or when <a href="#setLocalData">setLocalData</a>() is
172called again.
173<p> Note: QThreadStorage can only store pointers. The <em>data</em>
174argument must be either a pointer to an object created on the heap
175(i.e. using <em>new</em>) or 0. You should not delete <em>data</em>
176yourself; QThreadStorage takes ownership and will delete the <em>data</em> itself.
177<p> <p>See also <a href="#localData">localData</a>() and <a href="#hasLocalData">hasLocalData</a>().
178
179<!-- eof -->
180<hr><p>
181This file is part of the <a href="index.html">Qt toolkit</a>.
182Copyright &copy; 1995-2007
183<a href="http://www.trolltech.com/">Trolltech</a>. All Rights Reserved.<p><address><hr><div align=center>
184<table width=100% cellspacing=0 border=0><tr>
185<td>Copyright &copy; 2007
186<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
187<td align=right><div align=right>Qt 3.3.8</div>
188</table></div></address></body>
189</html>
Note: See TracBrowser for help on using the repository browser.