| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information ([email protected])
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the documentation of the Qt Toolkit.
|
|---|
| 7 | **
|
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 9 | ** Commercial Usage
|
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 13 | ** a written agreement between you and Nokia.
|
|---|
| 14 | **
|
|---|
| 15 | ** GNU Lesser General Public License Usage
|
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 19 | ** packaging of this file. Please review the following information to
|
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 22 | **
|
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain
|
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|---|
| 26 | ** 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 are unsure which license is appropriate for your use, please
|
|---|
| 37 | ** contact the sales department at [email protected].
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | /*!
|
|---|
| 43 | \page threads.html
|
|---|
| 44 | \title Thread Support in Qt
|
|---|
| 45 | \ingroup architecture
|
|---|
| 46 | \brief A detailed discussion of thread handling in Qt.
|
|---|
| 47 |
|
|---|
| 48 | Qt provides thread support in the form of platform-independent
|
|---|
| 49 | threading classes, a thread-safe way of posting events, and
|
|---|
| 50 | signal-slot connections across threads. This makes it easy to
|
|---|
| 51 | develop portable multithreaded Qt applications and take advantage
|
|---|
| 52 | of multiprocessor machines. Multithreaded programming is also a
|
|---|
| 53 | useful paradigm for performing time-consuming operations without
|
|---|
| 54 | freezing the user interface of an application.
|
|---|
| 55 |
|
|---|
| 56 | Earlier versions of Qt offered an option to build the library
|
|---|
| 57 | without thread support. Since Qt 4.0, threads are always enabled.
|
|---|
| 58 |
|
|---|
| 59 | This document is intended for an audience that has knowledge of,
|
|---|
| 60 | and experience with, multithreaded applications. If you are new
|
|---|
| 61 | to threading see our \l{#reading}{Recommended Reading} list.
|
|---|
| 62 |
|
|---|
| 63 | Topics:
|
|---|
| 64 |
|
|---|
| 65 | \tableofcontents
|
|---|
| 66 |
|
|---|
| 67 | \section1 The Threading Classes
|
|---|
| 68 |
|
|---|
| 69 | Qt includes the following thread classes:
|
|---|
| 70 |
|
|---|
| 71 | \list
|
|---|
| 72 | \o QThread provides the means to start a new thread.
|
|---|
| 73 | \o QThreadStorage provides per-thread data storage.
|
|---|
| 74 | \o QThreadPool manages a pool of threads that run QRunnable objects.
|
|---|
| 75 | \o QRunnable is an abstract class representing a runnable object.
|
|---|
| 76 | \o QMutex provides a mutual exclusion lock, or mutex.
|
|---|
| 77 | \o QMutexLocker is a convenience class that automatically locks
|
|---|
| 78 | and unlocks a QMutex.
|
|---|
| 79 | \o QReadWriteLock provides a lock that allows simultaneous read access.
|
|---|
| 80 | \o QReadLocker and QWriteLocker are convenience classes that automatically
|
|---|
| 81 | lock and unlock a QReadWriteLock.
|
|---|
| 82 | \o QSemaphore provides an integer semaphore (a generalization of a mutex).
|
|---|
| 83 | \o QWaitCondition provides a way for threads to go to sleep until
|
|---|
| 84 | woken up by another thread.
|
|---|
| 85 | \o QAtomicInt provides atomic operations on integers.
|
|---|
| 86 | \o QAtomicPointer provides atomic operations on pointers.
|
|---|
| 87 | \endlist
|
|---|
| 88 |
|
|---|
| 89 | \note Qt's threading classes are implemented with native threading APIs;
|
|---|
| 90 | e.g., Win32 and pthreads. Therefore, they can be used with threads of the
|
|---|
| 91 | same native API.
|
|---|
| 92 |
|
|---|
| 93 | \section2 Creating a Thread
|
|---|
| 94 |
|
|---|
| 95 | To create a thread, subclass QThread and reimplement its
|
|---|
| 96 | \l{QThread::run()}{run()} function. For example:
|
|---|
| 97 |
|
|---|
| 98 | \snippet doc/src/snippets/threads/threads.h 0
|
|---|
| 99 | \codeline
|
|---|
| 100 | \snippet doc/src/snippets/threads/threads.cpp 0
|
|---|
| 101 | \snippet doc/src/snippets/threads/threads.cpp 1
|
|---|
| 102 | \dots
|
|---|
| 103 | \snippet doc/src/snippets/threads/threads.cpp 2
|
|---|
| 104 |
|
|---|
| 105 | Then, create an instance of the thread object and call
|
|---|
| 106 | QThread::start(). The code that appears in the
|
|---|
| 107 | \l{QThread::run()}{run()} reimplementation will then be executed
|
|---|
| 108 | in a separate thread. Creating threads is explained in more
|
|---|
| 109 | detail in the QThread documentation.
|
|---|
| 110 |
|
|---|
| 111 | Note that QCoreApplication::exec() must always be called from the
|
|---|
| 112 | main thread (the thread that executes \c{main()}), not from a
|
|---|
| 113 | QThread. In GUI applications, the main thread is also called the
|
|---|
| 114 | GUI thread because it's the only thread that is allowed to
|
|---|
| 115 | perform GUI-related operations.
|
|---|
| 116 |
|
|---|
| 117 | In addition, you must create the QApplication (or
|
|---|
| 118 | QCoreApplication) object before you can create a QThread.
|
|---|
| 119 |
|
|---|
| 120 | \section2 Synchronizing Threads
|
|---|
| 121 |
|
|---|
| 122 | The QMutex, QReadWriteLock, QSemaphore, and QWaitCondition
|
|---|
| 123 | classes provide means to synchronize threads. While the main idea
|
|---|
| 124 | with threads is that they should be as concurrent as possible,
|
|---|
| 125 | there are points where threads must stop and wait for other
|
|---|
| 126 | threads. For example, if two threads try to access the same
|
|---|
| 127 | global variable simultaneously, the results are usually
|
|---|
| 128 | undefined.
|
|---|
| 129 |
|
|---|
| 130 | QMutex provides a mutually exclusive lock, or mutex. At most one
|
|---|
| 131 | thread can hold the mutex at any time. If a thread tries to
|
|---|
| 132 | acquire the mutex while the mutex is already locked, the thread will
|
|---|
| 133 | be put to sleep until the thread that currently holds the mutex
|
|---|
| 134 | unlocks it. Mutexes are often used to protect accesses to shared
|
|---|
| 135 | data (i.e., data that can be accessed from multiple threads
|
|---|
| 136 | simultaneously). In the \l{Reentrancy and Thread-Safety} section
|
|---|
| 137 | below, we will use it to make a class thread-safe.
|
|---|
| 138 |
|
|---|
| 139 | QReadWriteLock is similar to QMutex, except that it distinguishes
|
|---|
| 140 | between "read" and "write" access to shared data and allows
|
|---|
| 141 | multiple readers to access the data simultaneously. Using
|
|---|
| 142 | QReadWriteLock instead of QMutex when it is possible can make
|
|---|
| 143 | multithreaded programs more concurrent.
|
|---|
| 144 |
|
|---|
| 145 | QSemaphore is a generalization of QMutex that protects a certain
|
|---|
| 146 | number of identical resources. In contrast, a mutex protects
|
|---|
| 147 | exactly one resource. The \l{threads/semaphores}{Semaphores}
|
|---|
| 148 | example shows a typical application of semaphores: synchronizing
|
|---|
| 149 | access to a circular buffer between a producer and a consumer.
|
|---|
| 150 |
|
|---|
| 151 | QWaitCondition allows a thread to wake up other threads when some
|
|---|
| 152 | condition has been met. One or many threads can block waiting for
|
|---|
| 153 | a QWaitCondition to set a condition with
|
|---|
| 154 | \l{QWaitCondition::wakeOne()}{wakeOne()} or
|
|---|
| 155 | \l{QWaitCondition::wakeAll()}{wakeAll()}. Use
|
|---|
| 156 | \l{QWaitCondition::wakeOne()}{wakeOne()} to wake one randomly
|
|---|
| 157 | selected event or \l{QWaitCondition::wakeAll()}{wakeAll()} to
|
|---|
| 158 | wake them all. The \l{threads/waitconditions}{Wait Conditions}
|
|---|
| 159 | example shows how to solve the producer-consumer problem using
|
|---|
| 160 | QWaitCondition instead of QSemaphore.
|
|---|
| 161 |
|
|---|
| 162 | Note that Qt's synchronization classes rely on the use of properly
|
|---|
| 163 | aligned pointers. For instance, you cannot use packed classes with
|
|---|
| 164 | MSVC.
|
|---|
| 165 |
|
|---|
|
|---|