Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. My tutorial on how to properly use QThreads
QtWS25 Last Chance

My tutorial on how to properly use QThreads

Scheduled Pinned Locked Moved General and Desktop
34 Posts 14 Posters 45.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MayaPosch
    wrote on 23 Feb 2012, 16:57 last edited by
    #1

    Last year I posted a tutorial on how to use QThreads in 99% of all cases. The suggested approach in the documentation is to sub-class QThread, which is unneeded, inefficient and error-prone. My tutorial shows how to use QThreads without sub-classing and with proper resource management when the thread exits.

    Link: http://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/

    I based this tutorial on the (in)famous Nokia blog post and a few nearly-complete tutorials as well as my own experiments. I have received many positive responses and thank-yous to it since I put it online so I figured I'd put it up here as well :)

    Hope it's useful.

    Maya

    1 Reply Last reply
    1
    • ? This user is from outside of this forum
      ? This user is from outside of this forum
      Guest
      wrote on 23 Feb 2012, 17:08 last edited by
      #2

      You should have put that in "Qt in education"

      EDIT: Oh I see this is your first post, welcome to the forum :)

      1 Reply Last reply
      0
      • J Offline
        J Offline
        JohnKaul
        wrote on 23 Feb 2012, 17:57 last edited by
        #3

        That was an awesome read! Thank you Maya.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          MayaPosch
          wrote on 23 Feb 2012, 17:59 last edited by
          #4

          [quote author="ddriver" date="1330016886"]You should have put that in "Qt in education"

          EDIT: Oh I see this is your first post, welcome to the forum :)[/quote]

          Yeah, I'm just a silly n00b here :) Not a n00b Qt developer, though ;) If this post has to be moved, then that's fine :)

          Thanks for the welcome! :) And I'm glad you liked it, JohnKaul.

          1 Reply Last reply
          0
          • J Offline
            J Offline
            JohnKaul
            wrote on 23 Feb 2012, 18:09 last edited by
            #5

            Yes, it was very good. I have read quite a few of your other articles, now, as well.

            ...I need more. Please, write, faster.

            1 Reply Last reply
            0
            • ? This user is from outside of this forum
              ? This user is from outside of this forum
              Guest
              wrote on 23 Feb 2012, 18:37 last edited by
              #6

              [quote author="MayaPosch" date="1330019969"]
              Yeah, I'm just a silly n00b here :) Not a n00b Qt developer, though ;) If this post has to be moved, then that's fine :)
              [/quote]

              Figured that out, newbs don't normally enter the forum with tutorials :)

              Good tutorial, wasn't new to me (I bother checking for doc notes) but will probably be new to many.

              BTW, dunno if it is just my browser, but your code snippets lack indenting, which although not fatal makes code less readable. Thought I let you know.

              1 Reply Last reply
              0
              • K Offline
                K Offline
                KA51O
                wrote on 24 Feb 2012, 08:25 last edited by
                #7

                Its nothing new, but it has all the important information put together in a well written and comprehensive article. Nice work.

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  giesbert
                  wrote on 24 Feb 2012, 10:41 last edited by
                  #8

                  Would it be an option to also put it to our wiki? Then the devnet search would also find it and people looking there for ideas also? :-)
                  Its a good article.

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

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    MayaPosch
                    wrote on 24 Feb 2012, 15:40 last edited by
                    #9

                    @JohnKaul: here you go: just published http://mayaposch.wordpress.com/2012/02/24/implementing-a-cookiejar-for-qtwebkit-qnetworkcookiejar-analysis/ :)

                    @ddriver: Yeah, the WordPress WYSIWYG editor seems to love nuking indenting when I paste code into it. Found out about that recently. I'll correct it in this tutorial too.

                    @KA51O: Thank you :) I had a lot of trouble figuring out how to do it and spent weeks trying to make it work for my own projects. I figured I'd save others the trouble and agony :D

                    @Gerolf: I'd love to see it on the Wiki. Would save a lot of people the trouble of searching the entire 'net for this tutorial.

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      goetz
                      wrote on 24 Feb 2012, 23:48 last edited by
                      #10

                      Hi Maya,

                      welcome to Qt DevNet!

                      [quote author="MayaPosch" date="1330098044"]
                      @Gerolf: I'd love to see it on the Wiki. Would save a lot of people the trouble of searching the entire 'net for this tutorial.[/quote]

                      You can add everything yourself. The wiki is open for everyone here on DevNet, there are no editors or the like (despite the fact that everyone can edit your article, of course). I'm looking forward to read from you. Regarding your threads post, we already have a very good wiki article on the topic: "Threads, Events and QObjects":/wiki/Threads_Events_QObjects - I think your samples and explanations would fit very good into that article, instead of a new article.

                      http://www.catb.org/~esr/faqs/smart-questions.html

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        MayaPosch
                        wrote on 26 Feb 2012, 21:36 last edited by
                        #11

                        Hi Volker :)

                        I could massively edit that article, I guess... or maybe there's a more subtle way to go about it? Being new here I would like some guidance :)

                        1 Reply Last reply
                        0
                        • G Offline
                          G Offline
                          goetz
                          wrote on 26 Feb 2012, 22:55 last edited by
                          #12

                          A massive edit wouldn't be the best way to go. You might want to contact "peppe":/member/5319, the original author of said wiki article, directly to discuss improvements. I didn't compare your text an the wiki page, but I could guess that you come to the same conclusions, basically. Maybe you want to start to incorporate your code samples as a start.

                          If in doubt, you can alway start a new thread in the "Wiki Discussion":/forums/viewforum/16/ forum. And don't forget: The wiki has a history - if something goes seriously wrong, one can always roll back to a previous version.

                          http://www.catb.org/~esr/faqs/smart-questions.html

                          1 Reply Last reply
                          0
                          • A Offline
                            A Offline
                            andre
                            wrote on 27 Feb 2012, 08:32 last edited by
                            #13

                            Well, I don't think it hurts to have two entries on the same topic. I find that peppe's article is, while stuffed with good content, quite a hard article to chew through. Something a bit lighter would be welcome as an addition. I would recommend that you just publish your article as-is, and insert some links to the relevant sections in peppe's article for those looking for additional detail.

                            Anyway, MayaPosch, thanks for publishing this article!

                            1 Reply Last reply
                            0
                            • G Offline
                              G Offline
                              GentooXativa
                              wrote on 27 Feb 2012, 08:41 last edited by
                              #14

                              Nice read, sharing it. And welcome ^^

                              Jose Vicente Giner Sanchez - Senior Mobile Developer

                              www.gigigo.com

                              C/ Dr. Zamenhof 36bis, 1ÂșA 28027 Madrid
                              T: +34 917431436

                              1 Reply Last reply
                              0
                              • M Offline
                                M Offline
                                MayaPosch
                                wrote on 27 Feb 2012, 08:55 last edited by
                                #15

                                @Volker I think I agree with Andre on that it'd be better to keep my article as-is so as to keep it very focused on this particular topic (QThread). That way people can click through from peppe's article as said and it wouldn't put my article or peppe's out of context.

                                Shall I post this suggestion in the Wiki Discussion forum?

                                @Andre Thank you :) I like being passionate about software and good documentation ^_^

                                1 Reply Last reply
                                0
                                • A Offline
                                  A Offline
                                  andre
                                  wrote on 27 Feb 2012, 09:03 last edited by
                                  #16

                                  A note on the contents of the article (I don't feel like creating a Wordpress account to respond there).

                                  [quote]By the way, one extremely thing to note here is that you should NEVER allocate heap objects (using new) in the constructor of the QObject class as this allocation is then performed on the main thread and not on the new QThread instance, meaning that the newly created object is then owned by the main thread and not the QThread instance.[/quote]
                                  I think the statement above is false. As long as these objects have the worker object as their parent, they too will be moved to the new thread. From the QObject::moveToThread() documentation (my emphasis):

                                  [quote]Changes the thread affinity for this object and its children.[/quote]

                                  Funny enough, this is an issue with the old way of using QThread. People used to create new objects on the heap in their subclasses QThread class constructor, and those would obviously live in the main thread, not in the thread they wanted to start.

                                  What is noteworthy, but not explicitly mentioned in your article, is that the worker object cannot have a parent, as that would prevent the thread move.

                                  1 Reply Last reply
                                  0
                                  • M Offline
                                    M Offline
                                    MayaPosch
                                    wrote on 27 Feb 2012, 09:31 last edited by
                                    #17

                                    [quote author="Andre" date="1330333381"]A note on the contents of the article (I don't feel like creating a Wordpress account to respond there).

                                    I think the statement above is false. As long as these objects have the worker object as their parent, they too will be moved to the new thread. From the QObject::moveToThread() documentation (my emphasis):

                                    [quote]Changes the thread affinity for this object and its children.[/quote][/quote]

                                    Actually, no. I tried it the way you suggested before I wrote the tutorial, but it doesn't work. You do get interesting crashes that way, though, as the heap objects aren't moved :) They remain firmly owned by the main thread. Insert tongue-in-cheek comment here on the accuracy of the Qt documentation ;)

                                    (Disclaimer: I applied for a job as Qt Technical Writer, but there do not seem to be any open positions ATM :) )

                                    [quote]What is noteworthy, but not explicitly mentioned in your article, is that the worker object cannot have a parent, as that would prevent the thread move.
                                    [/quote]

                                    Well... some things should be obvious, right? I mean... :D

                                    1 Reply Last reply
                                    0
                                    • A Offline
                                      A Offline
                                      andre
                                      wrote on 27 Feb 2012, 09:44 last edited by
                                      #18

                                      [quote author="MayaPosch" date="1330335061"]
                                      [quote author="Andre" date="1330333381"]A note on the contents of the article (I don't feel like creating a Wordpress account to respond there).

                                      I think the statement above is false. As long as these objects have the worker object as their parent, they too will be moved to the new thread. From the QObject::moveToThread() documentation (my emphasis):

                                      [quote]Changes the thread affinity for this object and its children.[/quote][/quote]

                                      Actually, no. I tried it the way you suggested before I wrote the tutorial, but it doesn't work. You do get interesting crashes that way, though, as the heap objects aren't moved :) They remain firmly owned by the main thread. Insert tongue-in-cheek comment here on the accuracy of the Qt documentation ;)
                                      [/quote]
                                      Well, that would warrent a bugreport againt QtCore then. If I look at the sources (I checked 4.7.4) I see that children are moved. If they are not for you, then a bugreport with a sample to reproduce it would be in order.

                                      [quote]
                                      (Disclaimer: I applied for a job as Qt Technical Writer, but there do not seem to be any open positions ATM :) )

                                      [quote]What is noteworthy, but not explicitly mentioned in your article, is that the worker object cannot have a parent, as that would prevent the thread move.
                                      [/quote]

                                      Well... some things should be obvious, right? I mean... :D[/quote]
                                      I don't find it all that obvious, actually, and as you note it complicates the cleanup a bit. Your solution is just one of the ways that can be done, but if you want to recycle your worker objects, you need to come up with a different solution. However, as a basic example yours is a good solution.

                                      1 Reply Last reply
                                      0
                                      • G Offline
                                        G Offline
                                        goetz
                                        wrote on 28 Feb 2012, 15:17 last edited by
                                        #19

                                        [quote author="MayaPosch" date="1330332901"]@Volker I think I agree with Andre on that it'd be better to keep my article as-is so as to keep it very focused on this particular topic (QThread). That way people can click through from peppe's article as said and it wouldn't put my article or peppe's out of context.[/quote]

                                        Thinking about it, this is the better way to go. And it has the additional advantage that we can point users to that page if we need just a short sample of the new usage pattern of QThread :-)

                                        http://www.catb.org/~esr/faqs/smart-questions.html

                                        1 Reply Last reply
                                        0
                                        • M Offline
                                          M Offline
                                          MayaPosch
                                          wrote on 29 Feb 2012, 21:03 last edited by
                                          #20

                                          Alright, I shall put a new Wiki article together featuring the content of my blog article, then :)

                                          1 Reply Last reply
                                          0

                                          4/34

                                          23 Feb 2012, 17:59

                                          30 unread
                                          • Login

                                          • Login or register to search.
                                          4 out of 34
                                          • First post
                                            4/34
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved