<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- qthreadstorage.cpp --> <title>QThreadStorage Class | Qt Core 5.12.6</title> <link rel="stylesheet" type="text/css" href="style/offline-simple.css" /> <script type="text/javascript"> document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css"); // loading style sheet breaks anchors that were jumped to before // so force jumping to anchor again setTimeout(function() { var anchor = location.hash; // need to jump to different anchor first (e.g. none) location.hash = "#"; setTimeout(function() { location.hash = anchor; }, 0); }, 0); </script> </head> <body> <div class="header" id="qtdocheader"> <div class="main"> <div class="main-rounded"> <div class="navigationbar"> <table><tr> <td >Qt 5.12</td><td ><a href="qtcore-index.html">Qt Core</a></td><td ><a href="qtcore-module.html">C++ Classes</a></td><td >QThreadStorage</td></tr></table><table class="buildversion"><tr> <td id="buildversion" width="100%" align="right"><a href="qtcore-index.html">Qt 5.12.6 Reference Documentation</a></td> </tr></table> </div> </div> <div class="content"> <div class="line"> <div class="content mainContent"> <div class="sidebar"> <div class="toc"> <h3><a name="toc">Contents</a></h3> <ul> <li class="level1"><a href="#public-functions">Public Functions</a></li> <li class="level1"><a href="#details">Detailed Description</a></li> <li class="level2"><a href="#caveats">Caveats</a></li> </ul> </div> <div class="sidebar-content" id="sidebar-content"></div></div> <h1 class="title">QThreadStorage Class</h1> <!-- $$$QThreadStorage-brief --> <p>The <a href="qthreadstorage.html">QThreadStorage</a> class provides per-thread data storage. <a href="#details">More...</a></p> <!-- @@@QThreadStorage --> <div class="table"><table class="alignedsummary"> <tr><td class="memItemLeft rightAlign topAlign"> Header:</td><td class="memItemRight bottomAlign"> <span class="preprocessor">#include <QThreadStorage></span> </td></tr><tr><td class="memItemLeft rightAlign topAlign"> qmake:</td><td class="memItemRight bottomAlign"> QT += core</td></tr></table></div><ul> <li><a href="qthreadstorage-members.html">List of all members, including inherited members</a></li> </ul> <p><b>Note:</b> All functions in this class are thread-safe.</p> <a name="public-functions"></a> <h2 id="public-functions">Public Functions</h2> <div class="table"><table class="alignedsummary"> <tr><td class="memItemLeft rightAlign topAlign"> </td><td class="memItemRight bottomAlign"><b><a href="qthreadstorage.html#QThreadStorage">QThreadStorage</a></b>()</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> </td><td class="memItemRight bottomAlign"><b><a href="qthreadstorage.html#dtor.QThreadStorage">~QThreadStorage</a></b>()</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> bool </td><td class="memItemRight bottomAlign"><b><a href="qthreadstorage.html#hasLocalData">hasLocalData</a></b>() const</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> T &</td><td class="memItemRight bottomAlign"><b><a href="qthreadstorage.html#localData">localData</a></b>()</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> T </td><td class="memItemRight bottomAlign"><b><a href="qthreadstorage.html#localData-1">localData</a></b>() const</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qthreadstorage.html#setLocalData">setLocalData</a></b>(T <i>data</i>)</td></tr> </table></div> <a name="details"></a> <!-- $$$QThreadStorage-description --> <div class="descr"> <h2 id="details">Detailed Description</h2> <p>The <a href="qthreadstorage.html">QThreadStorage</a> class provides per-thread data storage.</p> <p><a href="qthreadstorage.html">QThreadStorage</a> is a template class that provides per-thread data storage.</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>().</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>If T is a pointer type, <a href="qthreadstorage.html">QThreadStorage</a> takes ownership of the data (which must be created on the heap with <code>new</code>) and deletes it when the thread exits, either normally or via termination.</p> <p>For example, the following code uses <a href="qthreadstorage.html">QThreadStorage</a> 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 class="cpp"> <span class="type"><a href="qthreadstorage.html#QThreadStorage">QThreadStorage</a></span><span class="operator"><</span><span class="type"><a href="qcache.html">QCache</a></span><span class="operator"><</span><span class="type"><a href="qstring.html">QString</a></span><span class="operator">,</span> SomeClass<span class="operator">></span> <span class="operator">></span> caches; <span class="type">void</span> cacheObject(<span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&</span>key<span class="operator">,</span> SomeClass <span class="operator">*</span>object) { caches<span class="operator">.</span>localData()<span class="operator">.</span>insert(key<span class="operator">,</span> object); } <span class="type">void</span> removeFromCache(<span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&</span>key) { <span class="keyword">if</span> (<span class="operator">!</span>caches<span class="operator">.</span>hasLocalData()) <span class="keyword">return</span>; caches<span class="operator">.</span>localData()<span class="operator">.</span>remove(key); } </pre> <a name="caveats"></a> <h3 id="caveats">Caveats</h3> <ul> <li>The <a href="qthreadstorage.html">QThreadStorage</a> destructor does not delete per-thread data. <a href="qthreadstorage.html">QThreadStorage</a> only deletes per-thread data when the thread exits or when <a href="qthreadstorage.html#setLocalData">setLocalData</a>() is called multiple times.</li> <li><a href="qthreadstorage.html">QThreadStorage</a> can be used to store data for the <code>main()</code> thread. <a href="qthreadstorage.html">QThreadStorage</a> deletes all data set for the <code>main()</code> thread when <a href="../qtwidgets/qapplication.html">QApplication</a> is destroyed, regardless of whether or not the <code>main()</code> thread has actually finished.</li> </ul> </div> <p><b>See also </b><a href="qthread.html">QThread</a>.</p> <!-- @@@QThreadStorage --> <div class="func"> <h2>Member Function Documentation</h2> <!-- $$$QThreadStorage[overload1]$$$QThreadStorage --> <h3 class="fn" id="QThreadStorage"><a name="QThreadStorage"></a>QThreadStorage::<span class="name">QThreadStorage</span>()</h3> <p>Constructs a new per-thread data storage object.</p> <!-- @@@QThreadStorage --> <!-- $$$~QThreadStorage[overload1]$$$~QThreadStorage --> <h3 class="fn" id="dtor.QThreadStorage"><a name="dtor.QThreadStorage"></a>QThreadStorage::<span class="name">~QThreadStorage</span>()</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><b>See also </b><a href="qthreadstorage.html#hasLocalData">hasLocalData</a>().</p> <!-- @@@~QThreadStorage --> <!-- $$$hasLocalData[overload1]$$$hasLocalData --> <h3 class="fn" id="hasLocalData"><a name="hasLocalData"></a><span class="type">bool</span> QThreadStorage::<span class="name">hasLocalData</span>() const</h3> <p>If T is a pointer type, returns <code>true</code> if the calling thread has non-zero data available.</p> <p>If T is a value type, returns whether the data has already been constructed by calling <a href="qthreadstorage.html#setLocalData">setLocalData</a> or <a href="qthreadstorage.html#localData">localData</a>.</p> <p><b>See also </b><a href="qthreadstorage.html#localData">localData</a>().</p> <!-- @@@hasLocalData --> <!-- $$$localData[overload1]$$$localData --> <h3 class="fn" id="localData"><a name="localData"></a><span class="type">T</span> &QThreadStorage::<span class="name">localData</span>()</h3> <p>Returns a reference to the data that was set by the calling thread.</p> <p>If no data has been set, this will create a default constructed instance of type T.</p> <p><b>See also </b><a href="qthreadstorage.html#setLocalData">setLocalData</a>() and <a href="qthreadstorage.html#hasLocalData">hasLocalData</a>().</p> <!-- @@@localData --> <!-- $$$localData$$$localData --> <h3 class="fn" id="localData-1"><a name="localData-1"></a><span class="type">T</span> QThreadStorage::<span class="name">localData</span>() 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><b>See also </b><a href="qthreadstorage.html#hasLocalData">hasLocalData</a>().</p> <!-- @@@localData --> <!-- $$$setLocalData[overload1]$$$setLocalDataT --> <h3 class="fn" id="setLocalData"><a name="setLocalData"></a><span class="type">void</span> QThreadStorage::<span class="name">setLocalData</span>(<span class="type">T</span> <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 T is a pointer type, <a href="qthreadstorage.html">QThreadStorage</a> takes ownership of the data and deletes it automatically either when the thread exits (either normally or via termination) or when setLocalData() is called again.</p> <p><b>See also </b><a href="qthreadstorage.html#localData">localData</a>() and <a href="qthreadstorage.html#hasLocalData">hasLocalData</a>().</p> <!-- @@@setLocalData --> </div> </div> </div> </div> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2019 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners.<br/> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.<br/> Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners. </p> </div> </body> </html>