Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Qt Academy Launch in California!

    Solved Creating/deleting *QThread on the stack

    General and Desktop
    qthread stack pointers
    3
    6
    1913
    Loading More Posts
    • 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.
    • R
      reezeus last edited by

      Hi There

      I would like to implement heavy calculations in separate threads.
      At present I'm already moving all the static heavy calculations in separate threads using the moveToThread method; by static, I mean the one I know in advance, when writing the code.
      I need to implement a function that performs quite important calculations when required. I would like to avoid to hard code all the possible threads as there can be more than a 100 (not in the same time).
      What I would like to do is to be able to create instances of QThread on the stack, and delete them once the slot "quit()" of the QThread is called. So I'm thinking of a QVector<QThread *>, but not sure how to handle it.

      Can you pls shed some light on this?

      Thanks

      1 Reply Last reply Reply Quote 0
      • JohanSolo
        JohanSolo last edited by JohanSolo

        Maybe the QtConcurrent namespace can help? Managing by hand such a high amount of threads might be a nightmare. Moreover having 100 threads performing heavy computation may be a performance killer. I remember somebody on the forum who implemented a neural network and used one thread per neuron. At the end having all neurons computed in one thread was faster...

        `They did not know it was impossible, so they did it.'
        -- Mark Twain

        1 Reply Last reply Reply Quote 2
        • patrikd
          patrikd last edited by

          hi reezeus,
          Have a look at QThreadpool. Helps to manage many threads in an effective way.
          best,
          patrik

          1 Reply Last reply Reply Quote 3
          • R
            reezeus last edited by

            I forgot QtConcurrent indeed, I use only QThread for a while now, and didn't think about it.

            Just had a look at QThreadPool and seems to be exactly what I need. I'll implement it this way and come back here with my feedback.

            Thanks.

            1 Reply Last reply Reply Quote 0
            • R
              reezeus last edited by

              I've implemented QThreadPool, performance is now incredibly faster. Thanks a lot.
              Now I'm actually wondering if using QThread has a real interest. Managing QThread can be very painful sometimes (to know when the thread has finished to work, if there's still something running in it or not, delete it properly as well as the class called in the thread, etc...).
              Is it a good practice to use QThreadPool even if I have only one thread?

              JohanSolo 1 Reply Last reply Reply Quote 0
              • JohanSolo
                JohanSolo @reezeus last edited by

                @reezeus said:

                Is it a good practice to use QThreadPool even if I have only one thread?

                I've never used QThreadPool but I can imagine that it eases scaling if you do so... The day you want to increase the number of threads, simply have a bigger pool. On the other hand if you know that one thread is what you need and will continue to need, QThread seems more reasonable IMHO.

                `They did not know it was impossible, so they did it.'
                -- Mark Twain

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post