File: qtconcurrentrun.html

package info (click to toggle)
qt4-x11 4%3A4.8.2%2Bdfsg-11
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 701,696 kB
  • sloc: cpp: 2,686,179; ansic: 375,485; python: 25,859; sh: 19,349; xml: 17,091; perl: 14,765; yacc: 5,383; asm: 5,038; makefile: 1,259; lex: 555; ruby: 526; objc: 347; cs: 112; pascal: 112; php: 54; sed: 34
file content (149 lines) | stat: -rw-r--r-- 14,857 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- qtconcurrentrun.cpp -->
  <title>Qt 4.8: &lt;QtConcurrentRun&gt; - Asynchronous Run</title>
  <link rel="stylesheet" type="text/css" href="style/offline.css" />
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="content"> 
    <a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a>
  </div>
  <div class="breadcrumb toolblock">
    <ul>
      <li class="first"><a href="index.html">Home</a></li>
      <!--  Breadcrumbs go here -->
    </ul>
  </div>
</div>
<div class="content mainContent">
<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-bound-function-arguments">Using Bound Function Arguments</a></li>
</ul>
</div>
<h1 class="title">&lt;QtConcurrentRun&gt; - Asynchronous Run</h1>
<span class="subtitle"></span>
<!-- $$$<QtConcurrentRun>-brief -->
<p>The &lt;QtConcurrentRun&gt; header provides a way to run a function in a separate thread. <a href="#details">More...</a></p>
<!-- @@@<QtConcurrentRun> -->
<ul>
</ul>
<a name="Functionsx"></a>
<h2>Functions</h2>
<table class="alignedsummary">
<tr><td class="memItemLeft topAlign rightAlign"> QFuture&lt;T&gt; </td><td class="memItemRight bottomAlign"><b><a href="qtconcurrentrun.html#run">run</a></b> ( Function <i>function</i>, ... )</td></tr>
</table>
<!-- $$$<QtConcurrentRun>-description -->
<div class="descr"> <a name="details"></a>
<p>This function is a part of the <a href="threads-qtconcurrent.html">Qt Concurrent</a> framework.</p>
<p>The <a href="qtconcurrentrun.html#run">QtConcurrent::run</a>() function runs a function in a separate thread. The return value of the function is made available through the <a href="qfuture.html">QFuture</a> API.</p>
<a name="running-a-function-in-a-separate-thread"></a>
<h2>Running a Function in a Separate Thread</h2>
<p>To run a function in another thread, use <a href="qtconcurrentrun.html#run">QtConcurrent::run</a>():</p>
<pre class="cpp"> <span class="keyword">extern</span> <span class="type">void</span> aFunction();
 <span class="type"><a href="qfuture.html">QFuture</a></span><span class="operator">&lt;</span><span class="type">void</span><span class="operator">&gt;</span> future <span class="operator">=</span> <span class="type"><a href="qtconcurrent.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="qthreadpool.html">QThreadPool</a>. You can use the <a href="qfuture.html">QFuture</a> and <a href="qfuturewatcher.html">QFutureWatcher</a> classes to monitor the status of the function.</p>
<a name="passing-arguments-to-the-function"></a>
<h2>Passing Arguments to the Function</h2>
<p>Passing arguments to the function is done by adding them to the <a href="qtconcurrentrun.html#run">QtConcurrent::run</a>() 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="qstring.html">QString</a></span> <span class="operator">&amp;</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="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="qfuture.html">QFuture</a></span><span class="operator">&lt;</span><span class="type">void</span><span class="operator">&gt;</span> future <span class="operator">=</span> <span class="type"><a href="qtconcurrent.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 <a href="qtconcurrentrun.html#run">QtConcurrent::run</a>() is called, and these values are passed to the thread when it begins executing the function. Changes made to the arguments after calling <a href="qtconcurrentrun.html#run">QtConcurrent::run</a>() are <i>not</i> visible to the thread.</p>
<a name="returning-values-from-the-function"></a>
<h2>Returning Values from the Function</h2>
<p>Any return value from the function is available via <a href="qfuture.html">QFuture</a>:</p>
<pre class="cpp"> <span class="keyword">extern</span> <span class="type"><a href="qstring.html">QString</a></span> functionReturningAString();
 <span class="type"><a href="qfuture.html">QFuture</a></span><span class="operator">&lt;</span><span class="type"><a href="qstring.html">QString</a></span><span class="operator">&gt;</span> future <span class="operator">=</span> <span class="type"><a href="qtconcurrent.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="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="qstring.html">QString</a></span> someFunction(<span class="keyword">const</span> <span class="type"><a href="qbytearray.html">QByteArray</a></span> <span class="operator">&amp;</span>input);

 <span class="type"><a href="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="qfuture.html">QFuture</a></span><span class="operator">&lt;</span><span class="type"><a href="qstring.html">QString</a></span><span class="operator">&gt;</span> future <span class="operator">=</span> <span class="type"><a href="qtconcurrent.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="qstring.html">QString</a></span> result <span class="operator">=</span> future<span class="operator">.</span>result();</pre>
<p>Note that the <a href="qfuture.html#result">QFuture::result</a>() function blocks and waits for the result to become available. Use <a href="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>Additional API Features</h2>
<a name="using-member-functions"></a>
<h3>Using Member Functions</h3>
<p><a href="qtconcurrentrun.html#run">QtConcurrent::run</a>() 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="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&lt;QByteArray&gt;  QByteArray::split(char sep) const' in a separate thread</span>
 <span class="type"><a href="qbytearray.html">QByteArray</a></span> bytearray <span class="operator">=</span> <span class="string">&quot;hello world&quot;</span>;
 <span class="type"><a href="qfuture.html">QFuture</a></span><span class="operator">&lt;</span><span class="type"><a href="qlist.html">QList</a></span><span class="operator">&lt;</span><span class="type"><a href="qbytearray.html">QByteArray</a></span><span class="operator">&gt;</span> <span class="operator">&gt;</span> future <span class="operator">=</span> <span class="type"><a href="qtconcurrent.html">QtConcurrent</a></span><span class="operator">::</span>run(bytearray<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="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="qlist.html">QList</a></span><span class="operator">&lt;</span><span class="type"><a href="qbytearray.html">QByteArray</a></span><span class="operator">&gt;</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"><a href="qimage.html">QImage</a></span> image <span class="operator">=</span> <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>;
 <span class="type"><a href="qfuture.html">QFuture</a></span><span class="operator">&lt;</span><span class="type">void</span><span class="operator">&gt;</span> future <span class="operator">=</span> <span class="type"><a href="qtconcurrent.html">QtConcurrent</a></span><span class="operator">::</span>run(<span class="operator">&amp;</span>image<span class="operator">,</span> <span class="operator">&amp;</span><span class="type"><a href="qimage.html">QImage</a></span><span class="operator">::</span>invertPixels<span class="operator">,</span> <span class="type"><a href="qimage.html">QImage</a></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-bound-function-arguments"></a>
<h3>Using Bound Function Arguments</h3>
<p>Note that Qt does not provide support for bound functions. This is provided by 3rd party libraries like <a href="http://www.boost.org/libs/bind/bind.html">Boost</a> or <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1836.pdf">C++ TR1 Library Extensions</a>.</p>
<p>You can use boost::bind() or std::tr1::bind() to <i>bind</i> a number of arguments to a function when called. There are number of reasons for doing this:</p>
<ul>
<li>To call a function that takes more than 5 arguments.</li>
<li>To simplify calling a function with constant arguments.</li>
<li>Changing the order of arguments.</li>
</ul>
<p>See the documentation for the relevant functions for details on how to use the bind API.</p>
<p>Calling a bound function is done like this:</p>
<pre class="cpp"> <span class="type">void</span> someFunction(<span class="type">int</span> arg1<span class="operator">,</span> <span class="type">double</span> arg2);
 <span class="type"><a href="qfuture.html">QFuture</a></span><span class="operator">&lt;</span><span class="type">void</span><span class="operator">&gt;</span> future <span class="operator">=</span> <span class="type"><a href="qtconcurrent.html">QtConcurrent</a></span><span class="operator">::</span>run(boost<span class="operator">::</span>bind(someFunction<span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">2.0</span>));
 <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span></pre>
</div>
<!-- @@@<QtConcurrentRun> -->
<h2>Function Documentation</h2>
<!-- $$$run[overload1]$$$runFunction... -->
<h3 class="fn"><a name="run"></a><span class="type"><a href="qfuture.html">QFuture</a></span>&lt;<span class="type">T</span>&gt; QtConcurrent::<span class="name">run</span> ( <span class="type">Function</span> <i>function</i>, ... )</h3>
<p>Runs <i>function</i> in a separate thread. The thread is taken from the global <a href="qthreadpool.html">QThreadPool</a>. Note that the function may not run immediately; the function will only be run when a thread is available.</p>
<p>T is the same type as the return value of <i>function</i>. Non-void return values can be accessed via the <a href="qfuture.html#result">QFuture::result</a>() function.</p>
<p>Note that the <a href="qfuture.html">QFuture</a> returned by QtConcurrent::run() does not support canceling, pausing, or progress reporting. The <a href="qfuture.html">QFuture</a> returned can only be used to query for the running/finished status and the return value of the function.</p>
<!-- @@@run -->
  <div class="ft">
    <span></span>
  </div>
</div> 
<div class="footer">
    <p>
      <acronym title="Copyright">&copy;</acronym> 2012 Nokia Corporation and/or its
      subsidiaries. Documentation contributions included herein are the copyrights of
      their respective owners.</p>
    <br />
    <p>
      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.</p>
    <p>
      Documentation sources may be obtained from <a href="http://www.qt-project.org">
      www.qt-project.org</a>.</p>
    <br />
    <p>
      Nokia, Qt and their respective logos are trademarks of Nokia Corporation 
      in Finland and/or other countries worldwide. All other trademarks are property
      of their respective owners. <a title="Privacy Policy"
      href="http://en.gitorious.org/privacy_policy/">Privacy Policy</a></p>
</div>
</body>
</html>