There are two way to use QThread:
- Subclass QThread and reimplement its run() function
- Use worker objects by moving them to the thread
As the QThread::run() is the entry point of worker thread, so the former usage is rather easy to understand.
In this article, we will try to figure out in which way the latter usage works.
Event Loop
As a event direvn programming framework, Qt make use of event loop widely. For example, following functions are used in nearly every Qt program.
1 2 3 4 5 6 |
|
Each of them will create a QEventLoop object, and run it. Take QCoreApplication as an example,
1 2 3 4 5 6 7 8 |
|
Conceptually, the event loop looks like this:
1 2 3 4 5 6 7 8 9 10 |
|
Each thread has its own event queue, note that, event queue is belong to thread instead of event loop, and it's shared by all the event loops running in this thread.
When the event loop find that its event queue is not empty, it will process the events one by one. Eventually, the QObject::event()
member of the target object get called.
Seems it's really not easy to understand how the event system works without a example. So we create a demo
Example
In this example,
First, we
- Create a custom Event
new QEvent(QEvent::User)
- Post the Event to a queue
QCoreApplication::postEvent()
Then,
- The Event is discovered by the event loop in the queue
QApplication::exec()
- The
Test::event()
get called by the event loop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
|
In this example, the Test::event()
get called in the main thread. What should we do if want to run it in a work thread??
Thread Affinity
As each thread have its own event queue, so there will be more than one event queues exists in one multi-thread program. So which event queue will be used when we post a event?
Let's have a look at the code of postEvent().
1 2 3 4 5 6 7 |
|
As you can see, the event queue is found through the receiver's thread property. This thread is called the thread affinity - what thread the QObject "lives" in. Normally, it's the thread in which the object was created, but it can be changed using QObject::moveToThread()
.
Please note that, QCoreApplication::postEvent()
is thread safe, as QMutex has been used here.
Now, it's easy to run the event process it worker thread instead of main thread.
Example
Add three lines to the main() function of last example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
The output of application will be
1 2 3 |
|
while the output of last example was
1 2 3 |
|
Queued Connection
For queued connection, when the signal is emitted, a event will be post to the event queue.
1 2 3 4 |
|
Then, this event will be found by the event queued, and finally, QObject::event() will be called in the thread.
1 2 3 4 5 6 |
|
As QCoreApplication::postEvent()
is thread safe, so if you interact with an object only using queued signal/slot connections, then the usual multithreading precautions need not to be taken any more.