<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- qtconcurrentrun.cpp --> <title>Concurrent Run | Qt Concurrent 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="qtconcurrent-index.html">Qt Concurrent</a></td><td >Concurrent Run</td></tr></table><table class="buildversion"><tr> <td id="buildversion" width="100%" align="right"><a href="qtconcurrent-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="#running-a-function-in-a-separate-thread">Running a Function in a Separate Thread</a></li> <li class="level1"><a href="#passing-arguments-to-the-function">Passing Arguments to the Function</a></li> <li class="level1"><a href="#returning-values-from-the-function">Returning Values from the Function</a></li> <li class="level1"><a href="#additional-api-features">Additional API Features</a></li> <li class="level2"><a href="#using-member-functions">Using Member Functions</a></li> <li class="level2"><a href="#using-lambda-functions">Using Lambda Functions</a></li> </ul> </div> <div class="sidebar-content" id="sidebar-content"></div></div> <h1 class="title">Concurrent Run</h1> <span class="subtitle"></span> <!-- $$$qtconcurrentrun.html-description --> <div class="descr"> <a name="details"></a> <p>The QtConcurrent::run() function runs a function in a separate thread. The return value of the function is made available through the <a href="../qtcore/qfuture.html">QFuture</a> API.</p> <p>This function is a part of the <a href="qtconcurrent-index.html">Qt Concurrent</a> framework.</p> <a name="running-a-function-in-a-separate-thread"></a> <h2 id="running-a-function-in-a-separate-thread">Running a Function in a Separate Thread</h2> <p>To run a function in another thread, use QtConcurrent::run():</p> <pre class="cpp"> <span class="keyword">extern</span> <span class="type">void</span> aFunction(); <span class="type"><a href="../qtcore/qfuture.html">QFuture</a></span><span class="operator"><</span><span class="type">void</span><span class="operator">></span> future <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>run(aFunction); </pre> <p>This will run <i>aFunction</i> in a separate thread obtained from the default <a href="../qtcore/qthreadpool.html">QThreadPool</a>. You can use the <a href="../qtcore/qfuture.html">QFuture</a> and <a href="../qtcore/qfuturewatcher.html">QFutureWatcher</a> classes to monitor the status of the function.</p> <p>To use a dedicated thread pool, you can pass the <a href="../qtcore/qthreadpool.html">QThreadPool</a> as the first argument:</p> <pre class="cpp"> <span class="keyword">extern</span> <span class="type">void</span> aFunction(); <span class="type"><a href="../qtcore/qthreadpool.html">QThreadPool</a></span> pool; <span class="type"><a href="../qtcore/qfuture.html">QFuture</a></span><span class="operator"><</span><span class="type">void</span><span class="operator">></span> future <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>run(<span class="operator">&</span>pool<span class="operator">,</span> aFunction); </pre> <a name="passing-arguments-to-the-function"></a> <h2 id="passing-arguments-to-the-function">Passing Arguments to the Function</h2> <p>Passing arguments to the function is done by adding them to the QtConcurrent::run() call immediately after the function name. For example:</p> <pre class="cpp"> <span class="keyword">extern</span> <span class="type">void</span> aFunctionWithArguments(<span class="type">int</span> arg1<span class="operator">,</span> <span class="type">double</span> arg2<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&</span>string); <span class="type">int</span> integer <span class="operator">=</span> <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>; <span class="type">double</span> floatingPoint <span class="operator">=</span> <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>; <span class="type"><a href="../qtcore/qstring.html">QString</a></span> string <span class="operator">=</span> <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>; <span class="type"><a href="../qtcore/qfuture.html">QFuture</a></span><span class="operator"><</span><span class="type">void</span><span class="operator">></span> future <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>run(aFunctionWithArguments<span class="operator">,</span> integer<span class="operator">,</span> floatingPoint<span class="operator">,</span> string); </pre> <p>A copy of each argument is made at the point where QtConcurrent::run() is called, and these values are passed to the thread when it begins executing the function. Changes made to the arguments after calling QtConcurrent::run() are <i>not</i> visible to the thread.</p> <a name="returning-values-from-the-function"></a> <h2 id="returning-values-from-the-function">Returning Values from the Function</h2> <p>Any return value from the function is available via <a href="../qtcore/qfuture.html">QFuture</a>:</p> <pre class="cpp"> <span class="keyword">extern</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> functionReturningAString(); <span class="type"><a href="../qtcore/qfuture.html">QFuture</a></span><span class="operator"><</span><span class="type"><a href="../qtcore/qstring.html">QString</a></span><span class="operator">></span> future <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>run(functionReturningAString); <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> result <span class="operator">=</span> future<span class="operator">.</span>result(); </pre> <p>As documented above, passing arguments is done like this:</p> <pre class="cpp"> <span class="keyword">extern</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> someFunction(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qbytearray.html">QByteArray</a></span> <span class="operator">&</span>input); <span class="type"><a href="../qtcore/qbytearray.html">QByteArray</a></span> bytearray <span class="operator">=</span> <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>; <span class="type"><a href="../qtcore/qfuture.html">QFuture</a></span><span class="operator"><</span><span class="type"><a href="../qtcore/qstring.html">QString</a></span><span class="operator">></span> future <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>run(someFunction<span class="operator">,</span> bytearray); <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> result <span class="operator">=</span> future<span class="operator">.</span>result(); </pre> <p>Note that the <a href="../qtcore/qfuture.html#result">QFuture::result</a>() function blocks and waits for the result to become available. Use <a href="../qtcore/qfuturewatcher.html">QFutureWatcher</a> to get notification when the function has finished execution and the result is available.</p> <a name="additional-api-features"></a> <h2 id="additional-api-features">Additional API Features</h2> <a name="using-member-functions"></a> <h3 >Using Member Functions</h3> <p>QtConcurrent::run() also accepts pointers to member functions. The first argument must be either a const reference or a pointer to an instance of the class. Passing by const reference is useful when calling const member functions; passing by pointer is useful for calling non-const member functions that modify the instance.</p> <p>For example, calling <a href="../qtcore/qbytearray.html#split">QByteArray::split</a>() (a const member function) in a separate thread is done like this:</p> <pre class="cpp"> <span class="comment">// call 'QList<QByteArray> QByteArray::split(char sep) const' in a separate thread</span> <span class="type"><a href="../qtcore/qbytearray.html">QByteArray</a></span> bytearray <span class="operator">=</span> <span class="string">"hello world"</span>; <span class="type"><a href="../qtcore/qfuture.html">QFuture</a></span><span class="operator"><</span><span class="type"><a href="../qtcore/qlist.html">QList</a></span><span class="operator"><</span><span class="type"><a href="../qtcore/qbytearray.html">QByteArray</a></span><span class="operator">></span> <span class="operator">></span> future <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>run(bytearray<span class="operator">,</span> <span class="operator">&</span><span class="type"><a href="../qtcore/qbytearray.html">QByteArray</a></span><span class="operator">::</span>split<span class="operator">,</span> <span class="char">','</span>); <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span> <span class="type"><a href="../qtcore/qlist.html">QList</a></span><span class="operator"><</span><span class="type"><a href="../qtcore/qbytearray.html">QByteArray</a></span><span class="operator">></span> result <span class="operator">=</span> future<span class="operator">.</span>result(); </pre> <p>Calling a non-const member function is done like this:</p> <pre class="cpp"> <span class="comment">// call 'void QImage::invertPixels(InvertMode mode)' in a separate thread</span> <span class="type">QImage</span> image <span class="operator">=</span> <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>; <span class="type"><a href="../qtcore/qfuture.html">QFuture</a></span><span class="operator"><</span><span class="type">void</span><span class="operator">></span> future <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>run(<span class="operator">&</span>image<span class="operator">,</span> <span class="operator">&</span><span class="type">QImage</span><span class="operator">::</span>invertPixels<span class="operator">,</span> <span class="type">QImage</span><span class="operator">::</span>InvertRgba); <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span> future<span class="operator">.</span>waitForFinished(); <span class="comment">// At this point, the pixels in 'image' have been inverted</span> </pre> <a name="using-lambda-functions"></a> <h3 >Using Lambda Functions</h3> <p>Calling a lambda function is done like this:</p> <pre class="cpp"> <span class="type"><a href="../qtcore/qfuture.html">QFuture</a></span><span class="operator"><</span><span class="type">void</span><span class="operator">></span> future <span class="operator">=</span> <span class="type"><a href="qtconcurrent-module.html">QtConcurrent</a></span><span class="operator">::</span>run(<span class="operator">[</span><span class="operator">=</span><span class="operator">]</span>() { <span class="comment">// Code in this block will run in another thread</span> }); <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span> </pre> </div> <!-- @@@qtconcurrentrun.html --> </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>