[556] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
| 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
---|
| 6 | **
|
---|
| 7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
| 8 | **
|
---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
| 10 | ** Commercial Usage
|
---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
| 14 | ** a written agreement between you and Nokia.
|
---|
| 15 | **
|
---|
| 16 | ** GNU Lesser General Public License Usage
|
---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
| 18 | ** General Public License version 2.1 as published by the Free Software
|
---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
| 20 | ** packaging of this file. Please review the following information to
|
---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
| 23 | **
|
---|
| 24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
| 25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
| 27 | **
|
---|
| 28 | ** GNU General Public License Usage
|
---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
| 32 | ** packaging of this file. Please review the following information to
|
---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
| 35 | **
|
---|
| 36 | ** If you have questions regarding the use of this file, please contact
|
---|
| 37 | ** Nokia at [email protected].
|
---|
| 38 | ** $QT_END_LICENSE$
|
---|
| 39 | **
|
---|
| 40 | ****************************************************************************/
|
---|
| 41 |
|
---|
| 42 | /*!
|
---|
| 43 | \page qt4-threads.html
|
---|
| 44 | \title Thread Support in Qt 4
|
---|
| 45 |
|
---|
| 46 | \contentspage {What's New in Qt 4}{Home}
|
---|
| 47 | \previouspage The Qt 4 Style API
|
---|
| 48 |
|
---|
| 49 | Qt 4 makes it easier than ever to write multithreaded
|
---|
| 50 | applications. More classes have been made usable from non-GUI
|
---|
| 51 | threads, and the signals and slots mechanism can now be used to
|
---|
| 52 | communicate between threads.
|
---|
| 53 |
|
---|
| 54 | \section1 General Overview
|
---|
| 55 |
|
---|
| 56 | QThread now inherits QObject. It emits signals to indicate that
|
---|
| 57 | the thread started or finished executing, and provides a few
|
---|
| 58 | slots as well.
|
---|
| 59 |
|
---|
| 60 | Each thread can now have its own event loop. The initial thread
|
---|
| 61 | starts its event loops using QCoreApplication::exec(); other
|
---|
| 62 | threads can start an event loop using QThread::exec(). Like
|
---|
| 63 | QCoreApplication, QThread also provides an
|
---|
| 64 | \l{QThread::exit()}{exit(int)} function and a
|
---|
| 65 | \l{QThread::quit()}{quit()} slot.
|
---|
| 66 |
|
---|
| 67 | An event loop in a thread makes it possible for the thread to use
|
---|
| 68 | certain non-GUI Qt classes that require the presence of an event
|
---|
| 69 | loop (such as QTimer, QTcpSocket, and QProcess). It also makes it
|
---|
| 70 | possible to connect signals from any threads to slots of a
|
---|
| 71 | specific thread. When a signal is emitted, the slot isn't called
|
---|
| 72 | immediately; instead, it is invoked when control returns to the
|
---|
| 73 | event loop of the thread to which the object belongs. The slot is
|
---|
| 74 | executed in the thread where the receiver object lives. See
|
---|
| 75 | QObject::connect() for details.
|
---|
| 76 |
|
---|
| 77 | Qt 4 also introduces a new synchronization class: QReadWriteLock.
|
---|
| 78 | It is similar to QMutex, except that it distinguishes between
|
---|
| 79 | "read" and "write" access to shared data and allows multiple
|
---|
| 80 | readers to access the data simultaneously. Using QReadWriteLock
|
---|
| 81 | instead of QMutex when it is possible can make multithreaded
|
---|
| 82 | programs more concurrent.
|
---|
| 83 |
|
---|
| 84 | Since Qt 4, \l{implicitly shared} classes can safely be copied
|
---|
| 85 | across threads, like any other value classes. They are fully
|
---|
| 86 | reentrant. This is implemented using atomic reference counting
|
---|
| 87 | operations, which are implemented in assembly language for the
|
---|
| 88 | different platforms supported by Qt. Atomic reference counting is
|
---|
| 89 | very fast, much faster than using a mutex.
|
---|
| 90 |
|
---|
| 91 | See \l{Thread Support in Qt} for more information.
|
---|
| 92 |
|
---|
| 93 | \section1 Comparison with Qt 3
|
---|
| 94 |
|
---|
| 95 | Earlier versions of Qt offered an option to build the library
|
---|
| 96 | without thread support. In Qt 4, threads are always enabled.
|
---|
| 97 |
|
---|
| 98 | Qt 3 had a class called \c QDeepCopy that you could use to take a
|
---|
| 99 | deep copy of an implicitly shared object. In Qt 4, the atomic
|
---|
| 100 | reference counting makes this class superfluous.
|
---|
| 101 | */
|
---|