[190] | 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"><!--
|
---|
| 8 | fn { margin-left: 1cm; text-indent: -1cm; }
|
---|
| 9 | a:link { color: #004faf; text-decoration: none }
|
---|
| 10 | a:visited { color: #672967; text-decoration: none }
|
---|
| 11 | body { 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 Classes</font></a>
|
---|
| 23 | | <a href="mainclasses.html">
|
---|
| 24 | <font color="#004faf">Main 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 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 <<a href="qthreadstorage-h.html">qthreadstorage.h</a>></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 & <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 data )</li>
|
---|
| 47 | </ul>
|
---|
| 48 | <hr><a name="details"></a><h2>Detailed Description</h2>
|
---|
| 49 |
|
---|
| 50 |
|
---|
| 51 | The QThreadStorage class provides per-thread data storage.
|
---|
| 52 | <p>
|
---|
| 53 |
|
---|
| 54 |
|
---|
| 55 | <p> QThreadStorage is a template class that provides per-thread data
|
---|
| 56 | storage.
|
---|
| 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
|
---|
| 59 | for the calling thread. The data can be accessed later using the
|
---|
| 60 | <a href="#localData">localData</a>() functions. QThreadStorage takes ownership of the
|
---|
| 61 | data (which must be created on the heap with <em>new</em>) and deletes
|
---|
| 62 | it when the thread exits (either normally or via termination).
|
---|
| 63 | <p> The <a href="#hasLocalData">hasLocalData</a>() function allows the programmer to determine if
|
---|
| 64 | data has previously been set using the setLocalData() function.
|
---|
| 65 | This is useful for lazy initializiation.
|
---|
| 66 | <p> For example, the following code uses QThreadStorage to store a
|
---|
| 67 | single cache for each thread that calls the <em>cacheObject()</em> and
|
---|
| 68 | <em>removeFromCache()</em> functions. The cache is automatically
|
---|
| 69 | deleted when the calling thread exits (either normally or via
|
---|
| 70 | termination).
|
---|
| 71 | <p> <pre>
|
---|
| 72 | QThreadStorage<QCache<SomeClass> *> caches;
|
---|
| 73 |
|
---|
| 74 | void cacheObject( const <a href="qstring.html">QString</a> &key, SomeClass *object )
|
---|
| 75 | {
|
---|
| 76 | if ( ! caches.hasLocalData() )
|
---|
| 77 | caches.setLocalData( new <a href="qcache.html">QCache</a><SomeClass> );
|
---|
| 78 |
|
---|
| 79 | caches.localData()->insert( key, object );
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | void removeFromCache( const <a href="qstring.html">QString</a> &key )
|
---|
| 83 | {
|
---|
| 84 | if ( ! caches.hasLocalData() )
|
---|
| 85 | return; // nothing to do
|
---|
| 86 |
|
---|
| 87 | caches.localData()->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
|
---|
| 95 | compiler limitations. Support for value-based objects will be
|
---|
| 96 | added when the majority of compilers are able to support partial
|
---|
| 97 | template specialization.
|
---|
| 98 | <p> <li> The <a href="#~QThreadStorage">destructor</a> does <em>not</em>
|
---|
| 99 | delete per-thread data. QThreadStorage only deletes per-thread
|
---|
| 100 | data when the thread exits or when <a href="#setLocalData">setLocalData</a>() is called
|
---|
| 101 | multiple 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
|
---|
| 104 | platform-specific APIs.
|
---|
| 105 | <p> <li> As a corollary to the above, platform-specific APIs cannot be
|
---|
| 106 | used to exit or terminate a QThread using QThreadStorage. Doing so
|
---|
| 107 | will cause all per-thread data to be leaked. See <a href="qthread.html#exit">QThread::exit</a>()
|
---|
| 108 | and <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>
|
---|
| 110 | thread <em>after</em> <a href="qapplication.html">QApplication</a> has been constructed. QThreadStorage
|
---|
| 111 | deletes all data set for the <em>main()</em> thread when QApplication is
|
---|
| 112 | destroyed, regardless of whether or not the <em>main()</em> thread has
|
---|
| 113 | actually finished.
|
---|
| 114 | <p> <li> The implementation of QThreadStorage limits the total number of
|
---|
| 115 | QThreadStorage objects to 256. An unlimited number of threads
|
---|
| 116 | can 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
|
---|
| 131 | in QThreadStorage is leaked. Make sure that all threads using
|
---|
| 132 | QThreadStorage 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;
|
---|
| 139 | otherwise returns FALSE.
|
---|
| 140 | <p> <p>See also <a href="#localData">localData</a>().
|
---|
| 141 |
|
---|
| 142 | <h3 class=fn>T & <a name="localData"></a>QThreadStorage::localData ()
|
---|
| 143 | </h3>
|
---|
| 144 |
|
---|
| 145 | <p> Returns a reference to the data that was set by the calling
|
---|
| 146 | thread.
|
---|
| 147 | <p> Note: QThreadStorage can only store pointers. This function
|
---|
| 148 | returns a <em>reference</em> to the pointer that was set by the calling
|
---|
| 149 | thread. The value of this reference is 0 if no data was set by
|
---|
| 150 | the 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 |
|
---|
| 156 | This 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
|
---|
| 159 | returns a pointer to the data that was set by the calling thread.
|
---|
| 160 | If 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 data )
|
---|
| 164 | </h3>
|
---|
| 165 |
|
---|
| 166 | <p> Sets the local data for the calling thread to <em>data</em>. It can be
|
---|
| 167 | accessed later using the <a href="#localData">localData</a>() functions.
|
---|
| 168 | <p> If <em>data</em> is 0, this function deletes the previous data (if
|
---|
| 169 | any) 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
|
---|
| 172 | called again.
|
---|
| 173 | <p> Note: QThreadStorage can only store pointers. The <em>data</em>
|
---|
| 174 | argument 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>
|
---|
| 176 | yourself; 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>
|
---|
| 181 | This file is part of the <a href="index.html">Qt toolkit</a>.
|
---|
| 182 | Copyright © 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 © 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>
|
---|