Discussion about "Threads, Events and QObjects" article
-
I think now I start understaing in the relevant "wiki page":http://qt-project.org/wiki/Threads_Events_QObjects . Thanks great articles!
After reading it second time while I'm translating it into Korean, I found following statement is quite ambiguous.
bq.
A thread event loop delivers events for all QObjects that are living in that thread; this includes, by default, all objects that are created into that thread, or that were moved to that thread (more info about this later). We also say that the thread affinity of a QObject is a certain thread, meaning that the object is living in that thread. This applies to objects which are built in the constructor of a QThread object:Because, any object that are built in the constructor of QThread couldn't live the thread represented by that QThread object, I feel like that the last statement sounds misleading doesn't it?
-
[quote author="Andre" date="1331800273"]Not really, as those are not created in that thread. Note that thread != QThread. The constructor of QThread runs in the same thread as the code that created the QThread object itself, not in the thread that QThread manages. [/quote]
Yeah, so the last statement might be "This doesn't apply to objects which are built in the constructorof a QThread object:" I thought.
-
No, I think you are misunderstanding this piece of text and the accociated piece of code. The fact that objects live in the thread that created them also applies to those objects created in the constructor of a subclassed QThread, where the thread is still the thread creating the object. If you read on, you will see an example that explains this further. The text below explains exactly this issue. So really, the text is correct.
-
So an object created in the constructor of a QThread (or subclass of QThread) is in the context of the thread that created the QThread object. Objects which are created in the run(..) method of the QThread object or which are moved to the context of the thread, which is managed by the QThread object, are in the context of that ("new") thread.
Correct ?
-
Thx 4 this great article!
There are still two things, which are not clear to me.
- How do I stop the thread's event loop, if it's executing an inifitely task (like reading data from serial port)? (It is not busy all the time, but it needs to run the event loop to be notified, when new data arrives).
Before reading this article I always did thread->moveToThread(thread); with the thread and then later called QMetaObject::invokeMethod(thread, "quit");
without moveToThread(thread) this won't work.
Maybe your wondering why I read the serial port from a different thread. The reason is, that when I'm moving the application's window with the mouse, the serial port won't receive any data until I release the window. It's probably some kind of bug in QextSerialPort, but I was able to fix it by using a thread for reading the serial port (but still event based).
- I'm not sure if I got it write, how it is determined, if Queued or DirectConnection is used.
My current understanding is that when using AutoConnection on every emit it is checked, if the thread which executes the emit statement is the same as the thread in which the objects whichs slot is going to be invoked lives in. If the threads are the same, then the slot is called directly, otherwise the invocation is queued.
Before reading this article I always thought, that at the moment at which QObject::connect with AutoConnection is called, it is determined, if a Direct or Queuedconnection is used and then this connection type will be used for all further emits.
Is the threadaffinity checked on every emit, or just when doing connect?
- How do I stop the thread's event loop, if it's executing an inifitely task (like reading data from serial port)? (It is not busy all the time, but it needs to run the event loop to be notified, when new data arrives).
-
in this section
[quote]
A much better and simpler way of achieving the same result is simply using timers, i.e. a QTimer [doc.qt.nokia.com] object with a 1s timeout, and make the doWork() method a slot:class Worker : public QObject
{
Q_OBJECTpublic:
Worker() {
connect(&timer, SIGNAL(timeout()), this, SLOT(doWork()));
timer.start(1000);
}private slots:
void doWork() {
/* ... */
}private:
QTimer timer;
};All we need is a running event loop, then the doWork() method will be invoked each second.
[/quote]
the timer needs to have its parent set to Worker instance otherwise it wont change thread when the owning class instance does. -
Hello Peppe, I've started revamping the official overview documentation for thread usage in Qt, and I'd like to incorporate some of the information from your article. Is that ok?
-
Is it OK to spawn new thread inside a readyRead() handler and then access and write to tcpsockets ( from the parent thread ) from that thread ? It looks to me that this is not good because sometimes clients are not receiving data . What is the proper way to do this ?
-
[quote author="great88" date="1376933337"]Is it OK to spawn new thread inside a readyRead() handler and then access and write to tcpsockets ( from the parent thread ) from that thread ? It looks to me that this is not good because sometimes clients are not receiving data . What is the proper way to do this ?[/quote]If your TCP socket is in the parent thread, you cannot write to it from a new thread.
There have been many posts about combining QTcpSocket and QThread recently. Search the forum, and you should find plenty of info.