21
28
HelloWorldTask *hello = new HelloWorldTask();
22
29
<span class="comment"> // QThreadPool takes ownership and deletes 'hello' automatically</span>
23
QThreadPool.globalInstance()->start(hello);</pre>
24
<p>QThreadPool deletes the <a href="qrunnable.html">QRunnable</a> automatically by default. Use <a href="qrunnable.html#setAutoDelete">QRunnable.setAutoDelete</a>() to change the auto-deletion flag.</p>
25
<p>QThreadPool supports executing the same <a href="qrunnable.html">QRunnable</a> more than once by calling tryStart(this) from within <a href="qrunnable.html#run">QRunnable.run</a>(). If autoDelete is enabled the <a href="qrunnable.html">QRunnable</a> will be deleted when the last thread exits the run function. Calling <a href="qthreadpool.html#start">start</a>() multiple times with the same <a href="qrunnable.html">QRunnable</a> when autoDelete is enabled creates a race condition and is not recommended.</p>
26
<p>Threads that are unused for a certain amount of time will expire. The default expiry timeout is 30000 milliseconds (30 seconds). This can be changed using <a href="qthreadpool.html#expiryTimeout-prop">setExpiryTimeout</a>(). Setting a negative expiry timeout disables the expiry mechanism.</p>
27
<p>Call <a href="qthreadpool.html#maxThreadCount-prop">maxThreadCount</a>() to query the maximum number of threads to be used. If needed, you can change the limit with <a href="qthreadpool.html#maxThreadCount-prop">setMaxThreadCount</a>(). The default <a href="qthreadpool.html#maxThreadCount-prop">maxThreadCount</a>() is <a href="qthread.html#idealThreadCount">QThread.idealThreadCount</a>(). The <a href="qthreadpool.html#activeThreadCount-prop">activeThreadCount</a>() function returns the number of threads currently doing work.</p>
28
<p>The <a href="qthreadpool.html#reserveThread">reserveThread</a>() function reserves a thread for external use. Use <a href="qthreadpool.html#releaseThread">releaseThread</a>() when your are done with the thread, so that it may be reused. Essentially, these functions temporarily increase or reduce the active thread count and are useful when implementing time-consuming operations that are not visible to the QThreadPool.</p>
29
<p>Note that QThreadPool is a low-level class for managing threads, see <a href="qtconcurrentrun.html#run">QtConcurrent.run</a>() or the other <a href="threads-qtconcurrent.html">Qt Concurrent</a> APIs for higher level alternatives.</p>
30
<p>See also <a href="qrunnable.html">QRunnable</a>.</p>
30
QThreadPool.globalInstance()->start(hello);
32
<p>QThreadPool deletes the <a href="qrunnable.html">QRunnable</a>
33
automatically by default. Use <a href="qrunnable.html#setAutoDelete">QRunnable.setAutoDelete</a>() to
34
change the auto-deletion flag.</p>
35
<p>QThreadPool supports executing the same <a href="qrunnable.html">QRunnable</a> more than once by calling
36
tryStart(this) from within <a href="qrunnable.html#run">QRunnable.run</a>(). If autoDelete is enabled
37
the <a href="qrunnable.html">QRunnable</a> will be deleted when the
38
last thread exits the run function. Calling <a href="qthreadpool.html#start">start</a>() multiple times with the same
39
<a href="qrunnable.html">QRunnable</a> when autoDelete is enabled
40
creates a race condition and is not recommended.</p>
41
<p>Threads that are unused for a certain amount of time will
42
expire. The default expiry timeout is 30000 milliseconds (30
43
seconds). This can be changed using <a href="qthreadpool.html#expiryTimeout-prop">setExpiryTimeout</a>().
44
Setting a negative expiry timeout disables the expiry
46
<p>Call <a href="qthreadpool.html#maxThreadCount-prop">maxThreadCount</a>() to
47
query the maximum number of threads to be used. If needed, you can
48
change the limit with <a href="qthreadpool.html#maxThreadCount-prop">setMaxThreadCount</a>(). The
49
default <a href="qthreadpool.html#maxThreadCount-prop">maxThreadCount</a>() is
50
<a href="qthread.html#idealThreadCount">QThread.idealThreadCount</a>().
51
The <a href="qthreadpool.html#activeThreadCount-prop">activeThreadCount</a>()
52
function returns the number of threads currently doing work.</p>
53
<p>The <a href="qthreadpool.html#reserveThread">reserveThread</a>()
54
function reserves a thread for external use. Use <a href="qthreadpool.html#releaseThread">releaseThread</a>() when your are
55
done with the thread, so that it may be reused. Essentially, these
56
functions temporarily increase or reduce the active thread count
57
and are useful when implementing time-consuming operations that are
58
not visible to the QThreadPool.</p>
59
<p>Note that QThreadPool is a low-level class for managing threads,
60
see <a href="qtconcurrentrun.html#run">QtConcurrent.run</a>() or
61
the other <a href="threads-qtconcurrent.html">Qt Concurrent</a>
62
APIs for higher level alternatives.</p>
31
63
<hr /><h2>Method Documentation</h2><h3 class="fn"><a name="QThreadPool" />QThreadPool.__init__ (<i>self</i>, <a href="qobject.html">QObject</a> <i>parent</i> = None)</h3><p>The <i>parent</i> argument, if not None, causes <i>self</i> to be owned by Qt instead of PyQt.</p><p>Constructs a thread pool with the given <i>parent</i>.</p>
32
<a name="//apple_ref/cpp/instm/QThreadPool/~QThreadPool" />
33
<h3 class="fn"><a name="activeThreadCount" />int QThreadPool.activeThreadCount (<i>self</i>)</h3><h3 class="fn"><a name="expiryTimeout" />int QThreadPool.expiryTimeout (<i>self</i>)</h3><h3 class="fn"><a name="globalInstance" /><a href="qthreadpool.html">QThreadPool</a> QThreadPool.globalInstance ()</h3><p>Returns the global <a href="qthreadpool.html">QThreadPool</a> instance.</p>
34
<a name="//apple_ref/cpp/instm/QThreadPool/releaseThread" />
66
<h3 class="fn"><a name="activeThreadCount" />int QThreadPool.activeThreadCount (<i>self</i>)</h3><h3 class="fn"><a name="expiryTimeout" />int QThreadPool.expiryTimeout (<i>self</i>)</h3><h3 class="fn"><a name="globalInstance" /><a href="qthreadpool.html">QThreadPool</a> QThreadPool.globalInstance ()</h3><p>Returns the global <a href="qthreadpool.html">QThreadPool</a>
35
70
<h3 class="fn"><a name="maxThreadCount" />int QThreadPool.maxThreadCount (<i>self</i>)</h3><h3 class="fn"><a name="releaseThread" />QThreadPool.releaseThread (<i>self</i>)</h3><p>Releases a thread previously reserved by a call to <a href="qthreadpool.html#reserveThread">reserveThread</a>().</p>
36
<p><b>Note:</b> Calling this function without previously reserving a thread temporarily increases <a href="qthreadpool.html#maxThreadCount-prop">maxThreadCount</a>(). This is useful when a thread goes to sleep waiting for more work, allowing other threads to continue. Be sure to call <a href="qthreadpool.html#reserveThread">reserveThread</a>() when done waiting, so that the thread pool can correctly maintain the <a href="qthreadpool.html#activeThreadCount-prop">activeThreadCount</a>().</p>
71
<p><b>Note:</b> Calling this function without previously reserving
72
a thread temporarily increases <a href="qthreadpool.html#maxThreadCount-prop">maxThreadCount</a>(). This
73
is useful when a thread goes to sleep waiting for more work,
74
allowing other threads to continue. Be sure to call <a href="qthreadpool.html#reserveThread">reserveThread</a>() when done
75
waiting, so that the thread pool can correctly maintain the
76
<a href="qthreadpool.html#activeThreadCount-prop">activeThreadCount</a>().</p>
37
77
<p>See also <a href="qthreadpool.html#reserveThread">reserveThread</a>().</p>
38
<a name="//apple_ref/cpp/instm/QThreadPool/reserveThread" />
39
<h3 class="fn"><a name="reserveThread" />QThreadPool.reserveThread (<i>self</i>)</h3><p>Reserves one thread, disregarding <a href="qthreadpool.html#activeThreadCount-prop">activeThreadCount</a>() and <a href="qthreadpool.html#maxThreadCount-prop">maxThreadCount</a>().</p>
40
<p>Once you are done with the thread, call <a href="qthreadpool.html#releaseThread">releaseThread</a>() to allow it to be reused.</p>
41
<p><b>Note:</b> This function will always increase the number of active threads. This means that by using this function, it is possible for <a href="qthreadpool.html#activeThreadCount-prop">activeThreadCount</a>() to return a value greater than <a href="qthreadpool.html#maxThreadCount-prop">maxThreadCount</a>() .</p>
80
<h3 class="fn"><a name="reserveThread" />QThreadPool.reserveThread (<i>self</i>)</h3><p>Reserves one thread, disregarding <a href="qthreadpool.html#activeThreadCount-prop">activeThreadCount</a>()
81
and <a href="qthreadpool.html#maxThreadCount-prop">maxThreadCount</a>().</p>
82
<p>Once you are done with the thread, call <a href="qthreadpool.html#releaseThread">releaseThread</a>() to allow it to
84
<p><b>Note:</b> This function will always increase the number of
85
active threads. This means that by using this function, it is
86
possible for <a href="qthreadpool.html#activeThreadCount-prop">activeThreadCount</a>()
87
to return a value greater than <a href="qthreadpool.html#maxThreadCount-prop">maxThreadCount</a>() .</p>
42
88
<p>See also <a href="qthreadpool.html#releaseThread">releaseThread</a>().</p>
43
<a name="//apple_ref/cpp/instm/QThreadPool/start" />
44
<h3 class="fn"><a name="setExpiryTimeout" />QThreadPool.setExpiryTimeout (<i>self</i>, int)</h3><h3 class="fn"><a name="setMaxThreadCount" />QThreadPool.setMaxThreadCount (<i>self</i>, int)</h3><h3 class="fn"><a name="start" />QThreadPool.start (<i>self</i>, <a href="qrunnable.html">QRunnable</a>, int <i>priority</i> = 0)</h3><p>Reserves a thread and uses it to run <i>runnable</i>, unless this thread will make the current thread count exceed <a href="qthreadpool.html#maxThreadCount-prop">maxThreadCount</a>(). In that case, <i>runnable</i> is added to a run queue instead. The <i>priority</i> argument can be used to control the run queue's order of execution.</p>
45
<p>Note that the thread pool takes ownership of the <i>runnable</i> if <a href="qrunnable.html#autoDelete">runnable->autoDelete()</a> returns true, and the <i>runnable</i> will be deleted automatically by the thread pool after the <a href="qrunnable.html#run">runnable->run()</a> returns. If <a href="qrunnable.html#autoDelete">runnable->autoDelete()</a> returns false, ownership of <i>runnable</i> remains with the caller. Note that changing the auto-deletion on <i>runnable</i> after calling this functions results in undefined behavior.</p>
46
<a name="//apple_ref/cpp/instm/QThreadPool/tryStart" />
91
<h3 class="fn"><a name="setExpiryTimeout" />QThreadPool.setExpiryTimeout (<i>self</i>, int)</h3><h3 class="fn"><a name="setMaxThreadCount" />QThreadPool.setMaxThreadCount (<i>self</i>, int)</h3><h3 class="fn"><a name="start" />QThreadPool.start (<i>self</i>, <a href="qrunnable.html">QRunnable</a>, int <i>priority</i> = 0)</h3><p>Reserves a thread and uses it to run <i>runnable</i>, unless
92
this thread will make the current thread count exceed <a href="qthreadpool.html#maxThreadCount-prop">maxThreadCount</a>(). In
93
that case, <i>runnable</i> is added to a run queue instead. The
94
<i>priority</i> argument can be used to control the run queue's
95
order of execution.</p>
96
<p>Note that the thread pool takes ownership of the <i>runnable</i>
97
if <a href="qrunnable.html#autoDelete">runnable->autoDelete()</a> returns
98
true, and the <i>runnable</i> will be deleted automatically by the
99
thread pool after the <a href="qrunnable.html#run">runnable->run()</a> returns. If <a href="qrunnable.html#autoDelete">runnable->autoDelete()</a> returns
100
false, ownership of <i>runnable</i> remains with the caller. Note
101
that changing the auto-deletion on <i>runnable</i> after calling
102
this functions results in undefined behavior.</p>
47
105
<h3 class="fn"><a name="tryStart" />bool QThreadPool.tryStart (<i>self</i>, <a href="qrunnable.html">QRunnable</a>)</h3><p>Attempts to reserve a thread to run <i>runnable</i>.</p>
48
<p>If no threads are available at the time of calling, then this function does nothing and returns false. Otherwise, <i>runnable</i> is run immediately using one available thread and this function returns true.</p>
49
<p>Note that the thread pool takes ownership of the <i>runnable</i> if <a href="qrunnable.html#autoDelete">runnable->autoDelete()</a> returns true, and the <i>runnable</i> will be deleted automatically by the thread pool after the <a href="qrunnable.html#run">runnable->run()</a> returns. If <a href="qrunnable.html#autoDelete">runnable->autoDelete()</a> returns false, ownership of <i>runnable</i> remains with the caller. Note that changing the auto-deletion on <i>runnable</i> after calling this function results in undefined behavior.</p>
50
<a name="//apple_ref/cpp/instm/QThreadPool/waitForDone" />
51
<h3 class="fn"><a name="waitForDone" />QThreadPool.waitForDone (<i>self</i>)</h3><p>Waits for each thread to exit and removes all threads from the thread pool.</p>
52
<p /><address><hr /><div align="center"><table border="0" cellspacing="0" width="100%"><tr class="address"><td align="left" width="25%">PyQt 4.7.7 for X11</td><td align="center" width="50%">Copyright © <a href="http://www.riverbankcomputing.com">Riverbank Computing Ltd</a> and <a href="http://www.qtsoftware.com">Nokia</a> 2010</td><td align="right" width="25%">Qt 4.6.3</td></tr></table></div></address></body></html>
b'\\ No newline at end of file'
106
<p>If no threads are available at the time of calling, then this
107
function does nothing and returns false. Otherwise, <i>runnable</i>
108
is run immediately using one available thread and this function
110
<p>Note that the thread pool takes ownership of the <i>runnable</i>
111
if <a href="qrunnable.html#autoDelete">runnable->autoDelete()</a> returns
112
true, and the <i>runnable</i> will be deleted automatically by the
113
thread pool after the <a href="qrunnable.html#run">runnable->run()</a> returns. If <a href="qrunnable.html#autoDelete">runnable->autoDelete()</a> returns
114
false, ownership of <i>runnable</i> remains with the caller. Note
115
that changing the auto-deletion on <i>runnable</i> after calling
116
this function results in undefined behavior.</p>
119
<h3 class="fn"><a name="waitForDone" />QThreadPool.waitForDone (<i>self</i>)</h3><p>Waits for each thread to exit and removes all threads from the
121
<address><hr /><div align="center"><table border="0" cellspacing="0" width="100%"><tr class="address"><td align="left" width="25%">PyQt 4.8.1 for X11</td><td align="center" width="50%">Copyright © <a href="http://www.riverbankcomputing.com">Riverbank Computing Ltd</a> and <a href="http://www.qtsoftware.com">Nokia</a> 2010</td><td align="right" width="25%">Qt 4.7.0</td></tr></table></div></address></body></html>
b'\\ No newline at end of file'