<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <!-- qthreadstorage.cpp --> <head> <title>Qt 4.6: QThreadStorage Class Reference</title> <link href="classic.css" rel="stylesheet" type="text/css" /> </head> <body> <a name="//apple_ref/cpp/cl//QThreadStorage"></a> <table border="0" cellpadding="0" cellspacing="0" width="100%"> <tr> <td align="left" valign="top" width="32"><a href="http://qt.nokia.com/"><img src="images/qt-logo.png" align="left" border="0" /></a></td> <td width="1"> </td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a> · <a href="classes.html"><font color="#004faf">All Classes</font></a> · <a href="functions.html"><font color="#004faf">All Functions</font></a> · <a href="overviews.html"><font color="#004faf">Overviews</font></a></td></tr></table><h1 class="title">QThreadStorage Class Reference<br /><span class="small-subtitle">[<a href="qtcore.html">QtCore</a> module]</span> </h1> <p>The QThreadStorage class provides per-thread data storage. <a href="#details">More...</a></p> <pre> #include <QThreadStorage></pre><p><b>Note:</b> All functions in this class are <a href="threads-reentrancy.html#thread-safe">thread-safe</a>.</p> <ul> <li><a href="qthreadstorage-members.html">List of all members, including inherited members</a></li> </ul> <hr /> <a name="public-functions"></a> <h2>Public Functions</h2> <table class="alignedsummary" border="0" cellpadding="0" cellspacing="0" width="100%"> <tr><td class="memItemLeft" align="right" valign="top"></td><td class="memItemRight" valign="bottom"><b><a href="qthreadstorage.html#QThreadStorage">QThreadStorage</a></b> ()</td></tr> <tr><td class="memItemLeft" align="right" valign="top"></td><td class="memItemRight" valign="bottom"><b><a href="qthreadstorage.html#dtor.QThreadStorage">~QThreadStorage</a></b> ()</td></tr> <tr><td class="memItemLeft" align="right" valign="top">bool </td><td class="memItemRight" valign="bottom"><b><a href="qthreadstorage.html#hasLocalData">hasLocalData</a></b> () const</td></tr> <tr><td class="memItemLeft" align="right" valign="top">T & </td><td class="memItemRight" valign="bottom"><b><a href="qthreadstorage.html#localData">localData</a></b> ()</td></tr> <tr><td class="memItemLeft" align="right" valign="top">T </td><td class="memItemRight" valign="bottom"><b><a href="qthreadstorage.html#localData-2">localData</a></b> () const</td></tr> <tr><td class="memItemLeft" align="right" valign="top">void </td><td class="memItemRight" valign="bottom"><b><a href="qthreadstorage.html#setLocalData">setLocalData</a></b> ( T <i>data</i> )</td></tr> </table> <a name="details"></a> <hr /> <h2>Detailed Description</h2> <p>The QThreadStorage class provides per-thread data storage.</p> <p>QThreadStorage is a template class that provides per-thread data storage.</p> <p><i>Note that due to compiler limitations, QThreadStorage can only store pointers.</i></p> <p>The <a href="qthreadstorage.html#setLocalData">setLocalData</a>() function stores a single thread-specific value for the calling thread. The data can be accessed later using <a href="qthreadstorage.html#localData">localData</a>(). QThreadStorage takes ownership of the data (which must be created on the heap with <tt>new</tt>) and deletes it when the thread exits, either normally or via termination.</p> <p>The <a href="qthreadstorage.html#hasLocalData">hasLocalData</a>() function allows the programmer to determine if data has previously been set using the <a href="qthreadstorage.html#setLocalData">setLocalData</a>() function. This is also useful for lazy initializiation.</p> <p>For example, the following code uses QThreadStorage 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.</p> <pre> QThreadStorage<QCache<QString, SomeClass> *> caches; void cacheObject(const QString &key, SomeClass *object) { if (!caches.hasLocalData()) caches.setLocalData(new QCache<QString, SomeClass>); caches.localData()->insert(key, object); } void removeFromCache(const QString &key) { if (!caches.hasLocalData()) return; caches.localData()->remove(key); }</pre> <a name="caveats"></a> <h3>Caveats</h3> <ul> <li>As noted above, QThreadStorage can only store pointers due to compiler limitations.</li> <li>The QThreadStorage destructor does not delete per-thread data. QThreadStorage only deletes per-thread data when the thread exits or when <a href="qthreadstorage.html#setLocalData">setLocalData</a>() is called multiple times.</li> <li>QThreadStorage can be used to store data for the <tt>main()</tt> thread. QThreadStorage deletes all data set for the <tt>main()</tt> thread when <a href="qapplication.html">QApplication</a> is destroyed, regardless of whether or not the <tt>main()</tt> thread has actually finished.</li> </ul> <p>See also <a href="qthread.html">QThread</a>.</p> <hr /> <h2>Member Function Documentation</h2> <a name="//apple_ref/cpp/instm/QThreadStorage/QThreadStorage"></a> <h3 class="fn"><a name="QThreadStorage"></a>QThreadStorage::QThreadStorage ()</h3> <p>Constructs a new per-thread data storage object.</p> <a name="//apple_ref/cpp/instm/QThreadStorage/~QThreadStorage"></a> <h3 class="fn"><a name="dtor.QThreadStorage"></a>QThreadStorage::~QThreadStorage ()</h3> <p>Destroys the per-thread data storage object.</p> <p>Note: The per-thread data stored is not deleted. Any data left in <a href="qthreadstorage.html">QThreadStorage</a> is leaked. Make sure that all threads using <a href="qthreadstorage.html">QThreadStorage</a> have exited before deleting the <a href="qthreadstorage.html">QThreadStorage</a>.</p> <p>See also <a href="qthreadstorage.html#hasLocalData">hasLocalData</a>().</p> <a name="//apple_ref/cpp/instm/QThreadStorage/hasLocalData"></a> <h3 class="fn"><a name="hasLocalData"></a>bool QThreadStorage::hasLocalData () const</h3> <p>Returns true if the calling thread has non-zero data available; otherwise returns false.</p> <p>See also <a href="qthreadstorage.html#localData">localData</a>().</p> <a name="//apple_ref/cpp/instm/QThreadStorage/localData"></a> <h3 class="fn"><a name="localData"></a>T & QThreadStorage::localData ()</h3> <p>Returns a reference to the data that was set by the calling thread.</p> <p>Note: <a href="qthreadstorage.html">QThreadStorage</a> 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,</p> <p>See also <a href="qthreadstorage.html#setLocalData">setLocalData</a>() and <a href="qthreadstorage.html#hasLocalData">hasLocalData</a>().</p> <h3 class="fn"><a name="localData-2"></a>T QThreadStorage::localData () const</h3> <p>This is an overloaded function.</p> <p>Returns a copy of the data that was set by the calling thread.</p> <p>Note: <a href="qthreadstorage.html">QThreadStorage</a> 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.</p> <p>See also <a href="qthreadstorage.html#hasLocalData">hasLocalData</a>().</p> <a name="//apple_ref/cpp/instm/QThreadStorage/setLocalData"></a> <h3 class="fn"><a name="setLocalData"></a>void QThreadStorage::setLocalData ( T <i>data</i> )</h3> <p>Sets the local data for the calling thread to <i>data</i>. It can be accessed later using the <a href="qthreadstorage.html#localData">localData</a>() functions.</p> <p>If <i>data</i> is 0, this function deletes the previous data (if any) and returns immediately.</p> <p>If <i>data</i> is non-zero, <a href="qthreadstorage.html">QThreadStorage</a> takes ownership of the <i>data</i> and deletes it automatically either when the thread exits (either normally or via termination) or when setLocalData() is called again.</p> <p>Note: <a href="qthreadstorage.html">QThreadStorage</a> can only store pointers. The <i>data</i> argument must be either a pointer to an object created on the heap (i.e. using <tt>new</tt>) or 0. You should not delete <i>data</i> yourself; <a href="qthreadstorage.html">QThreadStorage</a> takes ownership and will delete the <i>data</i> itself.</p> <p>See also <a href="qthreadstorage.html#localData">localData</a>() and <a href="qthreadstorage.html#hasLocalData">hasLocalData</a>().</p> <p /><address><hr /><div align="center"> <table width="100%" cellspacing="0" border="0"><tr class="address"> <td width="40%" align="left">Copyright © 2010 Nokia Corporation and/or its subsidiary(-ies)</td> <td width="20%" align="center"><a href="trademarks.html">Trademarks</a></td> <td width="40%" align="right"><div align="right">Qt 4.6.3</div></td> </tr></table></div></address></body> </html>