Discussion about "Threads, Events and QObjects" article

Page  
4

March 6, 2011

peppe peppe
Ant Farmer
1026 posts

Volker wrote:
The two articles are identical. My “version” (that without the underscores) is only a link to the actual article. The reason is, that the old version of the link is referred in some other articles and in a blog entry.

Unfortunately it does not redirect to the actual article but pulls in its content and it does not leave a message of doing so.

I totally missed what happened :) Thank you for setting everything up properly!

 Signature 

Software Engineer
KDAB (UK) Ltd., a KDAB Group company

March 12, 2012

James G James G
Lab Rat
2 posts

Sorry for taking a year to get back Peppe, but thanks for your reply at http://qt-project.org/forums/viewreply/25457/. I tend to get busy with big projects and don’t worry about past problems I’ve already fixed.

It took me a few minutes to get my head around what you were saying but after carefully re-reading the Qt documentation it finally hit me. It does make sense that if the objects are copied like you show in the first thread (or using a mutex if from another thread) then things are safe. This also says that passing a static copy of an object to another thread using a signal is safe because the copy operation happens on the original thread. Signaling with a pointer to the original object is another matter, however, and should not be done.

I guess my real gripe was the fact that QDateTime was not listed as implicitly shared when it really was. You can copy some static objects like QDate or QTime in the way I was doing without crashing because they only contain static members and are not implicitly shared. One might not get the right result due to synchronization problems with the underlying data, but it won’t cause a crash.

I will update my article soon with a link back to here.

March 15, 2012

joonhwan joonhwan
Lab Rat
94 posts

I think now I start understaing in the relevant wiki page [qt-project.org] . Thanks great articles!

After reading it second time while I’m translating it into Korean, I found following statement is quite ambiguous.

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?

 Signature 

joonhwan at gmail dot com

March 15, 2012

Andre Andre
Area 51 Engineer
6076 posts

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.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

March 16, 2012

joonhwan joonhwan
Lab Rat
94 posts
Andre wrote:
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.

Yeah, so the last statement might be “This doesn’t apply to objects which are built in the constructorof a QThread object:” I thought.

 Signature 

joonhwan at gmail dot com

March 16, 2012

Andre Andre
Area 51 Engineer
6076 posts

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.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

March 16, 2012

KA51O KA51O
Robot Herder
403 posts

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 ?

March 16, 2012

Andre Andre
Area 51 Engineer
6076 posts

Yes, that sounds correct.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

March 24, 2012

P@u1 P@u1
Lab Rat
1 posts

Thx 4 this great article!

There are still two things, which are not clear to me.

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

2. 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?

June 2, 2012

amleto amleto
Lab Rat
5 posts

in this section


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_OBJECT

public: Worker() { connect(&timer, SIGNAL), this, SLOT)); 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.


the timer needs to have its parent set to Worker instance otherwise it wont change thread when the owning class instance does.

November 14, 2012

JKSH JKSH
Dinosaur Breeder
722 posts

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?

Page  
4

  ‹‹ Wiki Addons with only links      [Suggestion] Wiki Comment ››

You must log in to post a reply. Not a member yet? Register here!