<tdalign="right"valign="center"><imgsrc="logo32.png"align="right"width="64"height="32"border="0"></td></tr></table><h1align=center>Thread Support in Qt</h1>
<p><!-- toc -->
<ul>
<li><ahref="#1"> Introduction
</a>
<li><ahref="#2"> Enabling Thread Support
</a>
<li><ahref="#3"> The Thread Classes
</a>
<li><ahref="#4"> Important Definitions
</a>
<li><ahref="#5"> Thread-safe Event Posting
</a>
<li><ahref="#6"> Threads and QObject subclasses
</a>
<li><ahref="#7"> The Qt Library Mutex
</a>
<li><ahref="#8"> Threads and Signals and Slots
</a>
<li><ahref="#9"> Threads and Shared Data
</a>
<li><ahref="#10"> Threads and the SQL Module
</a>
<li><ahref="#11"> Caveats
</a>
<li><ahref="#12"> Recommended Reading
</a>
</ul>
<!-- endtoc -->
<p><h2> Introduction
</h2>
<aname="1"></a><p> Qt provides thread support in the form of basic platform-independent
threading classes, a thread-safe way of posting events, and a global
Qt library lock that allows you to call Qt methods from different
threads.
<p> This document is intended for an audience that has knowledge of, and
experience with, multithreaded applications. If you are new to
threading see our <ahref="#reading">Recommended Reading</a> list.
<p><h2> Enabling Thread Support
</h2>
<aname="2"></a><p> When Qt is installed on Windows, thread support is an option on some
compilers.
<p> On Mac OS X and Unix, thread support is enabled by adding the
<tt>-thread</tt> option when running the <tt>configure</tt> script. On Unix
platforms where multithreaded programs must be linked in special ways,
such as with a special libc, installation will create a separate
library, <tt>libqt-mt</tt> and hence threaded programs must be linked
against this library (with <tt>-lqt-mt</tt>) rather than the standard Qt
<p> Any operations that generate events must not be called by any thread
other than the GUI thread. Examples of such operations are:
<p><ul>
<li> creating a <ahref="qwidget.html">QWidget</a>, <ahref="qtimer.html">QTimer</a>, <ahref="qsocketnotifier.html">QSocketNotifier</a>, <ahref="qsocket.html">QSocket</a> or other network class.
<li> moving, resizing, showing or hiding a QWidget.
<li> starting or stoping a QTimer.
<li> enabling or disabling a QSocketNotifier.
<li> using a QSocket or other network class.
</ul>
<p> Events generated by these operations will be lost on some platforms.
<p><h2> Threads and Signals and Slots
</h2>
<aname="8"></a><p> The Signals and Slots mechanism can be used in separate threads, as
long as the rules for <ahref="qobject.html">QObject</a> based classes are followed. The Signals
and Slots mechanism is synchronous: when a signal is emitted, all
slots are called immediately. The slots are executed in the thread
context that emitted the signal.
<p><b>Warning:</b> Slots that generate window system events or use window system
functions <em>must</em><em>not</em> be connected to a signal that is emitted from
a thread that is not the GUI thread. See the Qt Library Mutex section
above for more details.
<p><aname="threads-shared"></a>
<h2> Threads and Shared Data
</h2>
<aname="9"></a><p> Qt provides many <ahref="shclass.html#implicitly-shared">implicitly shared</a> and explicitly shared classes. In
a multithreaded program, multiple instances of a shared class can
reference shared data, which is dangerous if one or more threads
attempt to modify the data. Qt provides the <ahref="qdeepcopy.html">QDeepCopy</a> class, which
ensures that shared classes reference unique data.
<p> See the description of <ahref="shclass.html">implicit sharing</a> for more
information.
<p><aname="threads-sql"></a>
<h2> Threads and the SQL Module
</h2>
<aname="10"></a><p> A connection can only be used from within the thread that created it.
Moving connections between threads or creating queries from a different
thread is not supported.
<p> In addition, the third party libraries used by the QSqlDrivers can impose
further restrictions on using the SQL Module in a multithreaded program.
Consult the manual of your database client for more information.
<p><h2> Caveats
</h2>
<aname="11"></a><p> Some things to watch out for when programming with threads:
<p><ul>
<p><li> As mentioned above, <ahref="qobject.html">QObject</a> based classes are neither thread-safe
nor reentrant. This includes all widgets (e.g. <ahref="qwidget.html">QWidget</a> and
subclasses), OS kernel classes (e.g. <ahref="qprocess.html">QProcess</a>, <ahref="qaccel.html">QAccel</a>), and all
<p><li> Deleting a QObject while pending events are waiting to be delivered
will cause a crash. If you are creating QObjects in a thread that is
not the GUI thread and posting events to these objects, you should not
delete the QObject directly. Use the <ahref="qobject.html#deleteLater">QObject::deleteLater</a>() method
instead, which will cause the event loop to delete the object after
all pending events have been delivered to the object.
<p><li> Don't do any blocking operations while holding the Qt library
mutex. This will freeze up the event loop.
<p><li> Make sure you unlock a recursive <ahref="qmutex.html">QMutex</a> as many times as you lock
it, no more and no less.
<p><li> Don't mix the normal Qt library and the threaded Qt library in your
application. This means that if your application uses the threaded Qt
library, you should not link with the normal Qt library, dynamically
load the normal Qt library or dynamically load another library or
plugin that depends on the normal Qt library. On some systems, doing
this can corrupt the static data used in the Qt library.
<p><li> Qt does not support creating <ahref="qapplication.html">QApplication</a> and running the event
loop (with <ahref="qapplication.html#exec">QApplication::exec</a>()) in a secondary thread. You must
create the QApplication object and call QApplication::exec() from the
main() function in your program.
<p></ul>
<p><aname="reading"></a>
<h2> Recommended Reading
</h2>
<aname="12"></a><p><ul>
<li><ahref="http://www.amazon.com/exec/obidos/ASIN/0134436989/trolltech/t">Threads Primer: A Guide to Multithreaded Programming</a>
<li><ahref="http://www.amazon.com/exec/obidos/ASIN/0131900676/trolltech/t">Thread Time: The Multithreaded Programming Guide</a>
<li><ahref="http://www.amazon.com/exec/obidos/ASIN/1565921151/trolltech/t">Pthreads Programming: A POSIX Standard for Better Multiprocessing (O'Reilly Nutshell)</a>