Return to Linux Life Edit

  ホーム · 全てのクラス · メインのクラス · 注釈付き · グループ別 · 関数一覧

Qtにおけるスレッドサポート

Qt のスレッドサポートにより,プラットフォームに依存しないスレッドクラス,スレッドセーフなイベントのポスト,スレッドを跨いだ シグナル-スロット 接続 を利用できる. これにより,移植性の高いマルチスレッド対応の Qt アプリケーションを容易に開発でき,マルチプロセッサマシンの利点を享受できる. マルチスレッド対応のProgrammingは,アプリケーションのユーザインターフェースを停止させずに時間のかかる処理を行う場合にも使用される実用的なパラダイムである.

初期のバージョンの Qt ではスレッドサポートを無効にしたライブラリを構築するためのオプションが用意されていたが、 Qt 4.0 からはスレッドは常に利用可能である.

本文書はマルチスレッドアプリケーションに関して知識や経験を有する読者を対象としている. スレッドを使ったプログラミングについてあまり詳しくないのなら、 お勧めの書籍 を参照されたい.

トピックス:

スレッドクラス

Qt には以下のスレッドクラスが含まれる:

スレッドの作り方

スレッドを生成するためには、 QThread をサブクラス化して run() 関数を再実装する. たとえば次のとおり:

    class MyThread : public QThread
    {
        Q_OBJECT
    protected:
        void run();
    };
    void MyThread::run()
    {
        ...
    }

その後、スレッドオブジェクトのインスタンスを生成して QThread::start() を呼び出すことで、 run() の実装として書かれたコードが別のスレッド上で実行される. スレッドの生成に関する詳細については QThread のドキュメントに記す.

留意されたし: QCoreApplication::exec() は常にメインスレッド ( main()を実行するスレッド) から呼び出されなければならない. QThreadから呼び出すのではなく・・・。メインスレッドは GUI に関連した操作を実行することが許された唯一のスレッドであることから、GUI スレッドとも呼ばれる.

加えて、 QApplication (あるいは QCoreApplication) を生成しておく必要がある. QThreadを生成するよりも前に・・・。

スレッドの同期方法

QMutex, QReadWriteLock, QSemaphore, および QWaitCondition クラスはスレッドを同期させる手段を提供する. スレッドは原則として同時発生的であるので、スレッドを停止させて他のスレッドを待つようにするところが要点となる. たとえば、2つのスレッドが同じグローバル変数へ同時にアクセスしようとした場合、通常その結果は不定なものとなってしまう.

QMutex provides a mutually exclusive lock, or mutex. At most one thread can hold the mutex at any time. If a thread tries to acquire the mutex while the mutex already locked, the thread will be put to sleep until the thread that current holds the mutex unlocks it. Mutexes are often used to protect accesses to shared data (i.e., data that can be accessed from multiple threads simultaneously). In the Reentrancy and Thread-Safety section below, we will use it to make a class thread-safe.

QReadWriteLock is similar to QMutex, except that it distinguishes between "read" and "write" access to shared data and allows multiple readers to access the data simultaneously. Using QReadWriteLock instead of QMutex when it is possible can make multithreaded programs more concurrent.

QSemaphore is a generalization of QMutex that protects a certain number of identical resources. In contrast, a mutex protects exactly one resource. The Semaphores example shows a typical application of semaphores: synchronizing access to a circular buffer between a producer and a consumer.

QWaitCondition allows a thread to wake up other threads when some condition has been met. One or many threads can block waiting for a QWaitCondition to set a condition with wakeOne() or wakeAll(). Use wakeOne() to wake one randomly selected event or wakeAll() to wake them all. The Wait Conditions example shows how to solve the producer-consumer problem using QWaitCondition instead of QSemaphore.

Reentrancy and Thread-Safety

Throughout the Qt documentation, the terms reentrant and thread-safe are used to specify how a function can be used in multithreaded applications:

By extension, a class is said to be reentrant if any of its functions can be called simultaneously by multiple threads on different instances of the class, and thread-safe if it even works if the different threads operate on the same instance.

Note that the terminology in this domain isn't entirely standardized. POSIX uses a somewhat different definition of reentrancy and thread-safety for its C APIs. When dealing with an object-oriented C++ class library such as Qt, the definitions must be adapted.

Most C++ classes are inherently reentrant, since they typically only reference member data. Any thread can call such a member function on an instance of the class, as long as no other thread is calling a member function on the same instance. For example, the Counter class below is reentrant:

    class Counter
    {
    public:
        Counter() { n = 0; }
        void increment() { ++n; }
        void decrement() { --n; }
        int value() const { return n; }
    private:
        int n;
    };

The class isn't thread-safe, because if multiple threads try to modify the data member n, the result is undefined. This is because C++'s ++ and -- operators aren't necessarily atomic. Indeed, they usually expand to three machine instructions:

  1. Load the variable's value in a register.
  2. Increment or decrement the register's value.
  3. Store the register's value back into main memory.

If thread A and thread B load the variable's old value simultaneously, increment their register, and store it back, they end up overwriting each other, and the variable is incremented only once!

Clearly, the access must be serialized: Thread A must perform steps 1, 2, 3 without interruption (atomically) before thread B can perform the same steps; or vice versa. An easy way to make the class thread-safe is to protect all access to the data members with a QMutex:

    class Counter
    {
    public:
        Counter() { n = 0; }
        void increment() { QMutexLocker locker(&mutex); ++n; }
        void decrement() { QMutexLocker locker(&mutex); --n; }
        int value() const { QMutexLocker locker(&mutex); return n; }
    private:
        mutable QMutex mutex;
        int n;
    };

The QMutexLocker class automatically locks the mutex in its constructor and unlocks it when the destructor is invoked, at the end of the function. Locking the mutex ensures that access from different threads will be serialized. The mutex data member is declared with the mutable qualifier because we need to lock and unlock the mutex in value(), which is a const function.

Most Qt classes are reentrant and not thread-safe, to avoid the overhead of repeatedly locking and unlocking a QMutex. For example, QString is reentrant, meaning that you can use it in different threads, but you can't access the same QString object from different threads simultaneously (unless you protect it with a mutex yourself). A few classes and functions are thread-safe; these are mainly thread-related classes such as QMutex, or fundamental functions such as QCoreApplication::postEvent().

スレッドとQObjects

QThread inherits QObject. It emits signals to indicate that the thread started or finished executing, and provides a few slots as well.

More interesting is that QObjects can be used in multiple threads, emit signals that invoke slots in other threads, and post events to objects that live in other threads. This is possible because each thread is allowed to have its own event loop.

QObject Reentrancy

QObject is reentrant. Most of its non-GUI subclasses, such as QTimer, QTcpSocket, QUdpSocket, QHttp, QFtp, and QProcess, are also reentrant, making it possible to use these classes from multiple threads simultaneously. The only constraint is that you must ensure that all objects created in a thread are deleted before you delete the QThread. This can be done easily by creating the objects on the stack in your run() implementation.

On the other hand, the GUI classes, notably QWidget and all its subclasses, are not reentrant. They can only be used from the main thread. As noted earlier, QCoreApplication::exec() must also be called from that thread.

In practice, the impossibility of using GUI classes in other threads than the main thread can easily be worked around by putting time-consuming operations in a separate worker thread and displaying the results on screen in the main thread when the worker thread is finished. This is the approach used for implementing the Mandelbrot and the Blocking Fortune Client example.

Per-Thread Event Loop

Each thread can have its own event loop. The initial thread starts its event loops using QCoreApplication::exec(); other threads can start an event loop using QThread::exec(). Like QCoreApplication, QThread provides an exit(int) function and a quit() slot.

An event loop in a thread makes it possible for the thread to use certain non-GUI Qt classes that require the presence of an event loop (such as QTimer, QTcpSocket, and QProcess). It also makes it possible to connect signals from any threads to slots of a specific thread. This is explained in more detail in the Signals and Slots Across Threads section below.

A QObject instance is said to live in the thread in which it is created. Events to that object are dispatched by that thread's event loop. The thread in which a QObject lives is available using QObject::thread().

Calling delete on a QObject from another thread than the thread where it is created (or accessing the object in other ways) is unsafe unless you can guarantee that the object isn't processing events at the same moment. Use QObject::deleteLater() instead; it will post a DeferredDelete event, which the event loop of the object's thread will eventually pick up.

If no event loop is running, events won't be delivered to the object. For example, if you create a QTimer object in a thread but never call exec(), the QTimer will never emit its timeout() signal. Calling deleteLater() won't work either. (These restrictions apply to the main thread as well.)

You can manually post events to any object in any thread at any time using the thread-safe function QCoreApplication::postEvent(). The events will automatically be dispatched by the event loop of the thread where the object was created.

Event filters are supported in all threads, with the restriction that the monitoring object must live in the same thread as the monitored object. Similarly, QCoreApplication::sendEvent() (unlike postEvent()) can only be used to dispatch events to objects living in the thread from which the function is called.

Accessing QObject Subclasses from Other Threads

QObject and all of its subclasses are not thread-safe. This includes the entire event delivery system. It is important to keep in mind that the event loop may be delivering events to your QObject subclass while you are accessing the object from another thread.

If you are calling a function on an QObject subclass that doesn't live in the current thread and the object might receive events, you must protect all access to your QObject subclass's internal data with a mutex; otherwise, you may experience crashes or other undesired behavior.

Like other objects, QThread objects live in the thread where the object was created -- not in the thread that is created when QThread::run() is called. It is generally unsafe to provide slots in your QThread subclass, unless you protect the member variables with a mutex.

On the other hand, you can safely emit signals from your QThread::run() implementation, because signal emission is thread-safe.

Signals and Slots Across Threads

Qt supports two types of signal-slot connections:

By default, QObject::connect() establishes a direct connection if the sender and the receiver live in the same thread, and a queued connection if they live in different threads. This can be modified by passing an additional argument to connect(). Be aware that using direct connections when the sender and receiver don't live in the same thread is unsafe if an event loop is running in the receiver's thread, for the same reason that calling any function on an object living in another thread is unsafe.

QObject::connect() itself is thread-safe.

The Mandelbrot example uses a queued connection to communicate between a worker thread and the main thread. To avoid freezing the main thread's event loop (and, as a consequence, the application's user interface), all the Mandelbrot fractal computation is done in a separate worker thread. The thread emits a signal when it is done rendering the fractal.

Similarly, the Blocking Fortune Client example uses a separate thread for communicating with a TCP server asynchronously.

Threads and Implicit Sharing

Qt uses an optimization called implicit sharing for many of its value class, notably QImage and QString. In many people's minds, implicit sharing and multithreading are incompatible concepts, because of the way the reference counting is typically done. One solution is to protect the internal reference counter with a mutex, but this is prohibitively slow. Earlier versions of Qt didn't provide a satisfactory solution to this problem.

Beginning with Qt 4, implicit shared classes can safely be copied across threads, like any other value classes. They are fully reentrant. The implicit sharing is really implicit. This is implemented using atomic reference counting operations, which are implemented in assembly language for the different platforms supported by Qt. Atomic reference counting is very fast, much faster than using a mutex.

This having been said, if you access the same object in multiple threads simultaneously (as opposed to copies of the same object), you still need a mutex to serialize the accesses, just like with any reentrant class.

To sum it up, implicitly shared classes in Qt 4 are really implicitly shared. Even in multithreaded applications, you can safely use them as if they were plain, non-shared, reentrant classes.

Threads and the SQL Module

The classes in the SQL Module can be used in separate threads, as long as the rules for QObject based classes are followed.

The third-party libraries used to implement the SQL drivers can impose other restrictions on using the SQL Module in a multithreaded program. For example, the PostgreSQL library requires a separate connection per thread. Consult the documentation for your third-party library for more information.

Recommended Reading


Copyright © 2005 Trolltech Trademarks
Qt 4.0.0