CopperSpice API  1.9.1
Threads and QObjects

QThread inherits from QObject and emits signals to indicate the thread started or finished executing. QObjects can be used in multiple threads, emit signals which invoke slots in other threads, and post events to objects which belong to other threads. This is possible because each thread is allowed to have its own event loop.

QObject Conditionally Thread Safety

QObject is conditionally thread safe. Most of its non-GUI subclasses such as QTimer, QTcpSocket, QUdpSocket, QFtp, and QProcess, are also conditionally thread safe, making it possible to use these classes from multiple threads simultaneously. These classes are designed to be created and used from within a single thread and creating an object in one thread and calling its functions from another thread may be undefined behavior.

There are three constraints to be aware of:

  • The child of a QObject must always be created in the thread where the parent was created. This implies, among other things, that you should never pass the QThread object (this) as the parent of an object created in the thread (since the QThread object itself was created in another thread).
  • Event driven objects may only be used in a single thread. Specifically, this applies to the timer mechanism and the CsNetwork library. For example, you can not start a timer or connect a socket in a thread that is not the object's thread.
  • 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.

Although QObject is conditionally thread safe, the GUI classes like QWidget and all its subclasses, are not. They can only be used from the main thread. As noted earlier, QCoreApplication::exec() must also be called from the main thread.

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 CopperSpice 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.

Threads, objects, and event loops

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().

For QObjects that are created before QApplication, QObject::thread() returns zero. This means that the main thread will only handle posted events for these objects; other event processing is not done at all for objects with no thread. Use the QObject::moveToThread() function to change the thread affinity for an object and its children (the object can not be moved if it has a parent).

Calling delete on a QObject from a thread other than the one that owns the object (or accessing the object in other ways) is unsafe, unless you guarantee that the object is not processing events at that moment. Use QObject::deleteLater() instead, and a DeferredDelete event will be posted, which the event loop of the object's thread will eventually pick up. By default, the thread that owns a QObject is the thread that creates the QObject, but not after QObject::moveToThread() has been called.

If no event loop is running, events will not 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() will not 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 does not live in the current thread and the object might receive events, you must protect all access to your internal data with a mutex, otherwise, you may experience crashes or other undesired behavior.

As with other objects, QThread objects live in the thread where they are created. It is generally unsafe to provide slots in your QThread subclass, unless you protect the member variables with a mutex.

Signals can be emitted from any thread since emitting a signal does not cause a race condition or a dead lock.

Signals and Slots Across Threads

The connection type can be specified by passing it as an argument to the connect() method. Refer to Qt::ConnectionType for a list of supported connection types.

Using direct connections when the sender and receiver belong to different threads is unsafe if an event loop is running in the receiver's thread.