December 14, 2010

peppe peppe
Ant Farmer
1005 posts

Discussion about "Threads, Events and QObjects" article

Page  
1

Since the wiki apparently doesn’t support Discussion pages yet, I’m starting this thread to collect feedbacks about a (long) article I’ve written on the wiki these days about Threads and Events in Qt. The article itself is here: http://developer.qt.nokia.com/wiki/ThreadsEventsQObjects

Any comment, review or contribution is very welcome!

 Signature 

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

58 replies

December 14, 2010

peppe peppe
Ant Farmer
1005 posts

Gerolf Reinwardt wrote:
Hi Peppe,

As I can’t send you emails via the system, I try to give some feedback to the article this way:

I think “private messages” were somehow disabled, hope I’ve enabled them now…

First of all: Congratulations, a very good article. I also thought about writing something about this, but now you have :-))

Thank you so much :)


I just have some small extensions / add-ons:
  • In the chapter Events and the event loop you state, events are always asynchronous, thats true, if you don’t send them via QCoreApplication::sendEvent(…)

That’s true, but I don’t know how to bring that in. All in all, the article (as of now!) is not an in-depth review of Qt’s event system; along with sendEvent, many many things are missing (event filters, QCoreApplication::notify, how to create and send custom events, etc…)

  • Please mare a bigger warning on using QCoreApplication::processEvents(). I saw many applications crashing, because they used it and were surprised that the quit was executed although they are currently processing some events. And that happened esecially if tsendPostedEvents was called in a library…

What do you mean? Right now there’s a simple example by an hypotetic recursion into a slot. Do you think I should stressing on it even more?

  • Regrading the dialogs, they spin a local event loop :-)

I know, in fact there’s a note there.

  • Threads and Objects:
    • please state that the thread afinity depends on the running thread, that creates the object. If I create an object inside the QThread constructor, it depends on the creator’s thread. Often seen problems here :-)

Good point. Will do.

  • you could add that QObject::moveToThread() is a push, not a poll, which could make it a bit clearer, what is meant by “… we must use it from the thread the object is living in…”. Some of my colleges understood push and poll better..

Good point again; the Qt docs use a good lexicon :)

 Signature 

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

December 14, 2010

Gerolf Gerolf
Hobby Entomologist
3251 posts

What do you mean? Right now there’s a simple example by an hypotetic recursion into a slot. Do you think I should stressing on it even more?

I meant that it can not only fore recursion, it can also fore a stutdown or delete of an object, that you currently work on. Think of deleteLater, which is executed by the event loop. This can happen when you call @QApplication::processEvents@. And then, perhaps, an object, where yiou think it exists, is away. And I saw suxgh tghings, especially not recursions but unexpected object deletion which result in a crash. And then all you get is:
bq. It crashes, I don’t know why, but here the memory is freed…..

And you are the happy person to find the bug, they introduced by spinning the event loop out of scope…

So I would make a bigger note with many exclamation marks and warnings… :-))

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

December 15, 2010

Deleted Member # 4a2 Deleted Member # 4a2
Ant Farmer
1274 posts

moving this to Wiki forum, thats the right place to initiate discussions on wiki articles

December 15, 2010

peppe peppe
Ant Farmer
1005 posts

Gerolf Reinwardt wrote:
bq. What do you mean? Right now there’s a simple example by an hypotetic recursion into a slot. Do you think I should stressing on it even more?

I meant that it can not only fore recursion, it can also fore a stutdown or delete of an object, that you currently work on. Think of deleteLater, which is executed by the event loop. This can happen when you call @QApplication::processEvents@. And then, perhaps, an object, where yiou think it exists, is away. And I saw suxgh tghings, especially not recursions but unexpected object deletion which result in a crash. And then all you get is:
bq. It crashes, I don’t know why, but here the memory is freed…..

And you are the happy person to find the bug, they introduced by spinning the event loop out of scope…

So I would make a bigger note with many exclamation marks and warnings… :-))

Ok, I got it now :-)

Well, I’ve integrated your suggestions here and there. Give it a read if you want to :)

chetankjain wrote:
moving this to Wiki forum, thats the right place to initiate discussions on wiki articles

Ops! You’re right, sorry about that.

 Signature 

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

December 15, 2010

Gerolf Gerolf
Hobby Entomologist
3251 posts

Hi,
thanks again, the article is really good, and I think, the pitfalls are clearer now. So looking fporward to your next article :-))

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

December 20, 2010

Wolf P. Wolf P.
Ant Farmer
326 posts

how to start, stop, join a thread under (at least) one major operating system;

Is joining threads such a common pattern?
I’ve always imagined threads as autonomous agents. This I was able to produce programmatically and at some point they were finished and forgotten. Whether or not the program code orphaned never seemed to matter to me.

December 20, 2010

Gerolf Gerolf
Hobby Entomologist
3251 posts

Joining threads is common, if you put parts of a bigger thing to threads to do that in paralell and want to wait for all results being finished. I know, it can also be done with QtConcurrent but some programs are older than QtConcurrent :-))

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

December 20, 2010

peppe peppe
Ant Farmer
1005 posts

Wolf P. wrote:
bq. how to start, stop, join a thread under (at least) one major operating system;

Is joining threads such a common pattern?
I’ve always imagined threads as autonomous agents. This I was able to produce programmatically and at some point they were finished and forgotten. Whether or not the program code orphaned never seemed to matter to me.

It’s a pattern, that’s all. For instance, a possible use case is telling a worker thread to finish, then actually wait for it to end (by joining it), then deallocate some resources used by it.

 Signature 

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

December 20, 2010

Wolf P. Wolf P.
Ant Farmer
326 posts

Gerolf, I see. The somewhat outdated framework I worked with, provided only the forking.
Do you know a good real-world example?

Peppe, as I see, to join means simply to wait?

December 20, 2010

Gerolf Gerolf
Hobby Entomologist
3251 posts

Join means wait for finish, yes.
I have some in my work, but they are not open source :-)
removing objects, which are are internally multithreadded for example. You have to close all threads before removing the object.

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

December 20, 2010

peppe peppe
Ant Farmer
1005 posts

Wolf P. wrote:
Gerolf, I see. The somewhat outdated framework I worked with, provided only the forking.
Do you know a good real-world example?

Peppe, as I see, to join means simply to wait?

I was pretty sure it was standard lexicon when it comes to threading: it means “block the calling thread until the target thread terminates”; and yes, it’s what QThread::wait() does. See for instance:
http://www.kernel.org/doc/man-pages/online/pages/man3/pthread_join.3.html
http://download.oracle.com/javase/6/docs/api/java/lang/Thread.html#join()
http://perldoc.perl.org/threads.html#DESCRIPTION

Now that you’re telling me, perhaps should I change that term?

 Signature 

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

December 20, 2010

Volker Volker
Ant Farmer
5331 posts

I must admit, I did not read the wiki article, but have some experience with QThread. The term “join a thread” was unknown to me, so although this might be a common term in threading in general, it seems it is not used in Qt world.

December 20, 2010

Gerolf Gerolf
Hobby Entomologist
3251 posts

It is common in Boost threadding for example, that’s where I know it from. If it is a general term… ???

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

December 20, 2010

peppe peppe
Ant Farmer
1005 posts

I can put a footnote and/or a link there, just in case. The point is that you should know what QThread::wait() is for. What do you think?

 Signature 

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

December 20, 2010

Volker Volker
Ant Farmer
5331 posts

Knowledge about QThread::wait() is definitely needed.

maybe this wording is a bit more clear:

“how to start, stop, wait for a thread (aka join a thread in boost and others) under (at least) one major operating system”

Page  
1

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

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