Threads and QObjects▲
https:// inherits https://. It emits signals to indicate that the thread started or finished executing, and provides a few slots as well.
More interesting is that https://s 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▲
https:// is reentrant. Most of its non-GUI subclasses, such as https://, https://, https:// and https://, are also reentrant, making it possible to use these classes from multiple threads simultaneously. Note that these classes are designed to be created and used from within a single thread; creating an object in one thread and calling its functions from another thread is not guaranteed to work. There are three constraints to be aware of:
-
The child of a https:// must always be created in the thread where the parent was created. This implies, among other things, that you should never pass the https:// object (this) as the parent of an object created in the thread (since the https:// object itself was created in another thread).
-
Event driven objects may only be used in a single thread. Specifically, this applies to the https:// and the https://. For example, you cannot start a timer or connect a socket in a thread that is not the https://.
-
You must ensure that all objects created in a thread are deleted before you delete the https://. This can be done easily by creating the objects on the stack in your https://() implementation.
Although https:// is reentrant, the GUI classes, notably https:// and all its subclasses, are not reentrant. They can only be used from the main thread. As noted earlier, https://() 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 https:// and the Blocking Fortune Client example.
In general, creating QObjects before the https:// is not supported and can lead to weird crashes on exit, depending on the platform. This means static instances of https:// are also not supported. A properly structured single or multi-threaded application should make the https:// be the first created, and last destroyed https://.
Per-Thread Event Loop▲
Each thread can have its own event loop. The initial thread starts its event loop using https://(), or for single-dialog GUI applications, sometimes https://(). Other threads can start an event loop using https://(). Like https://, https:// provides an https://(int) function and a https://() 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 https://, https://, and https://). 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 https:// section below.
A https:// 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 https:// lives is available using https://().
The https://() function changes the thread affinity for an object and its children (the object cannot be moved if it has a parent).
Calling delete on a https:// 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 isn't processing events at that moment. Use https://() instead, and a https:// event will be posted, which the event loop of the object's thread will eventually pick up. By default, the thread that owns a https:// is the thread that creates the https://, but not after https://() has been called.
If no event loop is running, events won't be delivered to the object. For example, if you create a https:// object in a thread but never call https://(), the https:// will never emit its https://() signal. Calling https://() 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 https://(). 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, https://() (unlike https://()) 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▲
https:// 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 https:// subclass while you are accessing the object from another thread.
If you are calling a function on an https:// subclass that doesn't live in the current thread and the object might receive events, you must protect all access to your https:// subclass's internal data with a mutex; otherwise, you may experience crashes or other undesired behavior.
Like other objects, https:// objects live in the thread where the object was created – not in the thread that is created when https://() is called. It is generally unsafe to provide slots in your https:// subclass, unless you protect the member variables with a mutex.
On the other hand, you can safely emit signals from your https://() implementation, because signal emission is thread-safe.
Signals and Slots Across Threads▲
Qt supports these signal-slot connection types:
-
https:// (default) If the signal is emitted in the thread which the receiving object has affinity then the behavior is the same as the Direct Connection. Otherwise, the behavior is the same as the Queued Connection."
-
https:// The slot is invoked immediately, when the signal is emitted. The slot is executed in the emitter's thread, which is not necessarily the receiver's thread.
-
https:// The slot is invoked when control returns to the event loop of the receiver's thread. The slot is executed in the receiver's thread.
-
https:// The slot is invoked as for the Queued Connection, except the current thread blocks until the slot returns.
Using this type to connect objects in the same thread will cause deadlock.
-
https:// The behavior is the same as the Auto Connection, but the connection is made only if it does not duplicate an existing connection. i.e., if the same signal is already connected to the same slot for the same pair of objects, then the connection is not made and connect() returns false.
The connection type can be specified by passing an additional argument to https://(). Be aware that using direct connections when the sender and receiver live in different threads 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.
https://() 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 https:// uses a separate thread for communicating with a TCP server asynchronously.
▲
| https:// | https:// |



